1

I'm using the following code to generate an img url based on a drop down selection.

$(document).ready(function() {
    $('select').change(function(){
       var src = $(':selected', this).text()
       $('img').attr('src', location.hostname + "/" + src + '.jpg');
    });
});`

It works for showing adding the select text onto a static a url i.e. :

www.mysite.com/selecttext.jpg

But need to work for something like this:

www.mysite.com/<?php text value ?>/selecttext.jpg

The PHP is already functioning just need to know how to adjust the jquery to work.

4
  • Put your javascript in your PHP file, and do it like that then ? Commented Sep 1, 2012 at 17:13
  • edited with more specific and correct code Commented Sep 1, 2012 at 17:14
  • So the text value.. how is it defined? Commented Sep 1, 2012 at 17:17
  • using a magento command within a product loop <?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?> Commented Sep 1, 2012 at 17:31

2 Answers 2

1

PHP/HTML:

<script type="text/javascript"> var dir = '<?php echo "foo"; ?>'; </script>

JS:

$(document).ready(function() {
    $('select').change(function(){
       var src = $(':selected', this).text()
       $('img').attr('src', location.hostname + "/" + dir + '/' + src + '.jpg');
    });
});`
Sign up to request clarification or add additional context in comments.

1 Comment

That isn't rendering right: <img src="<script type="text/javascript"> var dir = '<?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?>'; </script>/.jpg" >
0

Remember that you can 'scavenge' other parts of the category page to add to the URL. At a guess you are looking for the product SKU to go in there - or at least the interal Magento ID. You can put this info in your category page - even if hidden (maybe as a title tag on the product name) - put an id on it and pull the value into your code.

The 'do it in php way' is the way Varien do it, Peter's way doesn't look too bad even if it is defining a global var (allegedly bad) - just pop it in your template code with $_item->getSku()

1 Comment

Thanks @IRIS Indigo. I'm trying to build img paths so I can swap out product images based on corresponding custom option downs. On a category page.

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.