0

I have a set of ids being generated in a foreach loop

<?php foreach ($_Collection as $_item): ?>
    <img class="<?php echo $_product->getId() ?>"  src="<?php echo Mage::helper('catalog/image')->init($_product, 'small_image')->resize(100, 75); ?>" />

I want to be able to grab specific divs with ajax by using the class that is generated as it will match both in this and the target document. So I was hoping to try something like this in the same foreach loop :

$(document).ready(function() {  
var item = "<?php echo $_product->getId() ?>";
$('img .'+item).click(function (){  
$('#result').load('ajax-page .'+item+')
    })

This is just for an example but I am sure there are multiple things wrong. For one what is printed looks like ...

var item = "156294";
$j('img .'+item).click(function (){ 
    $j('#result').load('ajax-page .item')
    });

For starters... how can I pass the item variable to the selector from within this loop?

4 Answers 4

1

I'd suggest changing your php script to add a class to your img file, and then use the name of your img for your item id. So, instead of

<img class="<?php echo $_product->getId() ?>"  src="<?php echo Mage::helper('catalog/image')->init($_product, 'small_image')->resize(100, 75); ?>" />

You would have something similar to

<img name="<?php echo $_product->getId() ?>" class="imgBlah"  src="<?php echo Mage::helper('catalog/image')->init($_product, 'small_image')->resize(100, 75); ?>" />

Then, use jQuery to attach a click event to any images with class 'imgBlah'. Using your provided script it would look something like this:

$(document).ready(function() {  

   $('img.imgBlah').click(function () {  
      $('#result').load('ajax-page .' + $(this).attr("name"))
   });

})
Sign up to request clarification or add additional context in comments.

Comments

0

You have a stray plus and quote at the end there after "item" in your code.

Comments

0

The idea looks right. But just a few things to point out:

  • You cannot have a class using a number e.g. ".156294" in $j('img .'+item) maybe append a letter first

  • what are you trying to achieve with load('ajax-page .item')? Maybe it should be load('ajax-page?id='+item)

1 Comment

Thanks Sebastian.. with .load you can grab a selective element so I am trying to grab just the divs with the class name = item
0

Your best option is to use "data-options" attribute on your image tags. Creating custom names/attributes/appending letters to stuff is tiresome. Data-options attribute is automatically parsed (by Jquery>1.4.3) into .data() json for that element. You can later easily access the information using jquery data() method. It also gives you an option of storing varied pieces of data without the need for post processing later

eg.

<img src='blah.png' data-role="page" data-hidden="true" data-options="{id:1234}">

You can later retrieve the data using this selector

$("img").data('options').id;

So in your example in your php loop when you write out images

<?php foreach ($_Collection as $_item): ?>
    <img class="clickme" data-options="{id:<?php echo $_product->getId() ?>}" src="<?php echo Mage::helper('catalog/image')->init($_product, 'small_image')->resize(100, 75); ?>" />

you jquery will than look like this - (you do not need to generate anything in php):

$(document).ready(function() {  
//this binds a click to all images with clickme class
$('.clickme').click(function (){
//this gets the id stored for that image
item = $(this).data('options').id
//and this does whatever with that id
$('#result').load('ajax-page.'+item)
    })

More on jquery data here http://api.jquery.com/data/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.