Pawan Dongol
Pawan Dongol

Reputation: 1158

how to pass string second parameter in js function of onclick button passed function?

I am googling from last one days but I never be found what's the problem in my code. My append js code is

      $('.dynamic-form').append("<div class='row " + field_type + "-field" + field_count + "'>" +
            "<button type='button' class='btn btn-primary' onClick='FieldEdit("+field_count + "," \''+field_type+ '\' +  ")'>" + ' + "</button>");

Js funtion is

function FieldEdit(id,type) {
    $('#' + type + '-field' + id).toggle();
}

I feel so much glad and thankful if any one solve out this issue. Thanks you!!!!

Upvotes: 0

Views: 143

Answers (1)

KAD
KAD

Reputation: 11102

You need to add the proper quotes with escape for the second parameter:

          ...      "<button type='button' class='btn btn-primary' 
onClick='FieldEdit(" + field_count + ",\"" + field_type + "\")'></button>");

Console output:

<button type='button' class='btn btn-primary' 
   onClick='FieldEdit(1,"This string")'></button>

Upvotes: 3

Related Questions