Sam Danger
Sam Danger

Reputation: 49

Using Jquery to adjust a dynamic url

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.

Upvotes: 1

Views: 275

Answers (2)

Theodores
Theodores

Reputation: 1209

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()

Upvotes: 0

Peter
Peter

Reputation: 16923

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');
    });
});`

Upvotes: 1

Related Questions