1

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.

pacific ray
  • 55
  • 2
  • 8

4 Answers4

0

Assuming you are trying to pass the reference of the clicked element to the delSchedule function, you need to change your href definition like this:

"href": function() {
    var myhref = "javascript:delSchedule(this);"; // note 'this' is part of the string, not concatenated
    return myhref;
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0
{
  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;
    }
}
TwilightTitus
  • 190
  • 1
  • 9
0

check the documentation for more examples but you should be using the built in jquery events like so

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

Note that json2html supports most jquery events by just adding the prefix "on" to the event .. eg onclick, onfocus, etc...

Chad Brown
  • 1,627
  • 1
  • 13
  • 22
  • "this" returns the button node inside the delSchedule() function. confirmed by console.log() inside delSchedule() function. What I need is the json piece passed to this transform; and is being rendered by this transform. – pacific ray Dec 17 '14 at 09:14
  • then use e.obj .. more info at json2html.com => Attributes => jquery => Event Attributes – Chad Brown Dec 17 '14 at 23:59
0

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.

pacific ray
  • 55
  • 2
  • 8