0

i am getting json file data on ready event

    $(document).ready(function(e){
        $.getJSON( "data/wall.json", function( data ) {
            createWall(data);
        });
    })

    function createWall(data)
    {
        for(var i=0; i<data.length;i++){
    var stats = $("<p class='meta'><span>14 Days Ago</span><span class='comments' onclick='commentSection("+data[i]+")'>0 Comments</span><span class='likes'>Like</span></p>");
    $('#wall').append(stats);
}
    }

    function commentSection(data){
    console.log(data);
    }

Here when i click on comments. It shows Unexpected identifier . How to pass particular data by onclick event

1
  • Everything working fine. I can't use onclick with arguments. But It called without argument Commented Oct 29, 2014 at 9:08

3 Answers 3

1

A more appropriate solution will be is to use event delegation and data api like

$(document).ready(function (e) {
    $.getJSON("data/wall.json", function (data) {
        createWall(data);
    });

    $('#wall').on('click', '.comments', function () {
        var data = $(this).closest('.meta').data('walldata');
        commentSection(data);
    });
})

function createWall(data) {
    for (var i = 0; i < data.length; i++) {
        var stats = $("<p class='meta'><span>14 Days Ago</span><span class='comments'>0 Comments</span><span class='likes'>Like</span></p>").data('walldata', data[i]);
        $('#wall').append(stats);
    }
}

function commentSection(data) {
    console.log(data);
}
Sign up to request clarification or add additional context in comments.

1 Comment

@MohaideenIsmail sorry... fixed a but in reading the data... it should have been var data = $(this).closest('.meta').data('walldata');
1

Try this : put quotes around data using escape character.

function createWall(data)
{
        for(var i=0; i<data.length;i++){
    var stats = $("<p class='meta'><span>14 Days Ago</span><span class='comments' onclick='commentSection(\""+data[i]+"\")'>0 Comments</span><span class='likes'>Like</span></p>");
    $('#wall').append(stats);
   }
}

4 Comments

It passing [object Object] . Not a values
to pass value you need to use data[i].property
@BhushanKawadkar , dont u think event delegation is better solution?
@jQueryAngryBird, yes it is better solution as suggested by Arun.
0

Slightly Changes by me, I have passed the data in data- attribute and define a event with .on() on .comments class :

var stats = $("<p class='meta'><span>14 Days Ago</span><span class='comments' data-clk='"+data[i]+"' >0 Comments</span><span class='likes'>Like</span></p>");

$('body').on('click', '.comments', function(e){
   e.preventDefault(); 

   // Retrive Data
   var data = $(this).data('clk');
})

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.