use JSON variable in jQuery dynamically

Posted by user1644123 on Stack Overflow See other posts from Stack Overflow or by user1644123
Published on 2012-12-12T10:45:33Z Indexed on 2012/12/12 11:03 UTC
Read the original article Hit count: 212

Filed under:
|
|
|
|

I have two DIVs, #placeholder AND #imageLoad. When the user clicks on a particular thumb its larger version (thumb2) should then appear in #imageLoad DIV.

Here is the jQuery that needs to be fixed:

  $.getJSON('jsonFile.json', function(data) {
        var output="<ul>";
        for (var i in data.items) {
            output+="<li><img src=images/items/" + data.items[i].thumb + ".jpg></li>";
        }
        output+="</ul>";
        document.getElementById("placeholder").innerHTML=output;
  });


  //This is wrong!! Not working..
  $('li').on({
         mouseenter: function() {
             document.getElementById("imageLoad").innerHTML="<img src=images/items/" +
             data.items[i].thumb2 + ".jpg>";
         }
  });    

Here is the external JSON file below (jsonFile.json):

{"items":[
    {
        "id":"1",
        "thumb":"01_sm",
        "thumb2":"01_md"
    },
    {
        "id":"2",
        "thumb":"02_sm",
        "thumb2":"02_md"
    }
]}

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery