Goran Stojanovic Goran Stojanovic - 2 months ago 19
Javascript Question

Why Jquery can't change color to all elements from foreach statement except first one?

Why JQuery can't add color to all elements from foreach loop except first one?
This is just basic testing.

Here is the PHP code.

<?php foreach($results as $row): ?>
<div class="col-sm-12" id="article_wrap">
<h3><?php echo anchor ('artikl/prikazi/'.$row->id, $row->naziv, array('id' => 'article_title'));?></h3>
<p id="article_description"> <?php echo $row->opis;?> </p>
<strong id="article_price"> <?php echo $row->cijena;?> KM </strong>
<h6 id="article_category"> <?php echo $row->kategorija;?> </h6>
</div>
<?php endforeach;?>


and Jquery code

<script>
$('#article_wrap').mouseenter(function(){
$(this).css('background-color' , 'red');
})
</script>

Answer

You can only use ids #article_wrap on one element. Use classes .article_wrap when used on multiple elements.

<?php foreach($results as $row): ?>
            <div class="col-sm-12 article_wrap">
            <h3><?php echo anchor ('artikl/prikazi/'.$row->id, $row->naziv, array('id' => 'article_title'));?></h3>
                <p class="article_description"> <?php echo $row->opis;?> </p>
                <strong class="article_price"> <?php echo $row->cijena;?> KM </strong>
                <h6 id="article_category"> <?php echo $row->kategorija;?> </h6>
            </div> 
 <?php endforeach;?>

<script>
    $('.article_wrap').mouseenter(function(){
        $(this).css('background-color' , 'red');
    })
</script>