Search code examples
javascriptjqueryhtmljson2html

rendering anchor href with json2html


I am trying to render an anchor with json2html with the following transform:

'renderTimeline':[  {
    tag: "a",  
    class: "btn btn-warning btn-circle", 
    style: "float: right;", 
    html: "<i class=\"icon-remove\"></i>",
    "href": function() {
        var myhref = "javascript:delSchedule(" + this + ");";
        return myhref;
    }
}]

intention is to delete the json object,which is passed to it with :

$('#sunTimeLine').json2html(sched1.sunday, transforms.renderTimeline, {'events':true});

I get the following as o/p on the rendered html:

<a class="btn btn-warning btn-circle" style="float: right;" href="javascript:delSchedule([object Object]);"><i class="icon-remove"></i></a>

when i click on the link(button) i get a message in browser console:

SyntaxError: missing ] after element list

Please help me solve this issue.


Solution

  • Following code solved my issue:

    'renderTimeline':[  {
        tag: "a",  
        class: "btn btn-warning btn-circle", 
        style: "float: right;", 
        html: "<i class=\"icon-remove\"></i>",
        "onclick": function(e) {
           delSchedule(e);
       }}
    }]
    

    If I am passing the following json :

    { monday:[ { startTime:10:00, endTime: 12:00, room_id:cse124 }, { startTime:13:00, endTime: 15:00, room_id:lotus } ] }
    

    I want to be able to access "monday" in the function delSchedule(). How do i do this? Please help.