Nagasai
Nagasai

Reputation: 11

onclick attribute not binding in html code while wrote inside a json data

i have to add dynamic onclick attribute while data adding dynamically.

var jsondata = {
        jsonarray : [
                {data : '<button onclick="function("param")">submit</button>'}
        ]
}

$('#id').html(jsondata.jsonarray[i].data);

onclick attribute no binded in html code. if is there any other alternative please provide me. anyone help me on this.

Upvotes: 0

Views: 39

Answers (1)

Barmar
Barmar

Reputation: 781058

The " before param is matching the " that starts the onclick attribute, so it's just onclick="function(". You need to use a different quote around the parameter than you use around the attribute.

var jsondata = {
  jsonarray: [{
    data: '<button onclick="function(\'param\')">submit</button>'
  }]
}

$('#id').html(jsondata.jsonarray[i].data);

Upvotes: 1

Related Questions