A
A
Aleksander9112015-12-20 11:01:31
PHP
Aleksander911, 2015-12-20 11:01:31

How to remove php from js?

Good day everyone. There is a code in js and php, js sends an event to different id in Google Map:

$("#card'.$id.'").each(function() {
          $(this).on("mouseenter", function() {
          if(infobox) {
            google.maps.event.trigger(Marker['.$id.'], "click");
          }
          });
          $(this).on("mouseleave", function() {
            infobox['.$id.'].open(mapGMap, Marker['.$id.']);
            });
          });

and
<div id="card<?php echo $item->id; ?>">Текст</div>
Tell me, is it possible to get rid of php in the javascript code and use, for example, a loop to execute the code?
Ps: id is a number

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey, 2015-12-20
@Aleksander911

Specify a class for the div and attach a handler to it. And id parameter.

<div class="card-item" item-id="<?php echo $item->id; ?>" id="card<?php echo $item->id; ?>">Текст</div>
<script>
$('.card-item').each(function(){
    var item_id = $(this).attr('item-id');
});
</script>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question