Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a JSON URL to a jQuery function?

I have this loaded before the radio buttons with corresponding color variable:

<script type="text/javascript">
    //<![CDATA[
    images = {"Black":"http:\/\/ecx.images-amazon.com\/images\/I\/31XwBA2m8pL.jpg", "Slate":"http:\/\/ecx.images-amazon.com\/images\/I\/31SqWkrSb6L.jpg", "White":"http:\/\/ecx.images-amazon.com\/images\/I\/31OJufgJT2L.jpg"};
    //]]>
</script>

Here is the jQuery function. The best I can do so far is make the first image disappear, leaving the alt attribute.

function change_large_product_image(url) {
    jQuery('.largeprod img').attr('src', url);
    return false;
}

Here is a sample source for the radio button.

<label for="Black"
       style="background: #fff url(http://ecx.images-amazon.com/images/I/11TeKFAzJsL._SL30_.jpg) center center no-repeat;"
       title="Black" >
    Black
</label>
<input
    id="color_black"
    name="color"
    onclick="change_large_product_image(images['Black'])"
    type="radio"
    value="Black" />
like image 407
Jesse Avatar asked Dec 30 '25 22:12

Jesse


1 Answers

What is the value of the parameter, url? It looks like you may need to decode the URL string to remove the '\' characters.

function change_large_product_image(url) {
    jQuery('.largeprod img').attr('src', decodeURI(url));
    return false;
}

In addition, consider taking thaBadDawg's suggestion.

like image 76
Chetan S Avatar answered Jan 02 '26 12:01

Chetan S



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!