0

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!!!!

5
  • console type and id , what are you getting Commented Jun 19, 2017 at 10:07
  • You are trying to select an element by its id. You have not shown any HTML element that would have an id, so we have no idea what you are actually trying to select here. Commented Jun 19, 2017 at 10:07
  • show this error SyntaxError: illegal character Commented Jun 19, 2017 at 10:08
  • What is field_count and field_type? What do want it to do? What are the results. What are the results you were expecting? Commented Jun 19, 2017 at 10:11
  • I just want field_count=1 and field_type should be in string which can be bus,truck,van etc which can be dynamically selected by Id and field_type of the function FieldEdit. but while I am consoling type it's show error. Commented Jun 19, 2017 at 10:27

1 Answer 1

3

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @KAD. This can show error while onclick function trigger. Error is like SyntaxError: expected expression, got '}'. while I am inspecting html in browser the html should be like this:- <button type="button" class="btn btn-primary" onclick="FieldEdit(1," bus')=""> Edit </button>
please try the latest edit, there was a problem with the original answer

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.