1

I wanna use an html in jquery.I use this code :

$(document).ready(function(){
    $("#registerNums").click(function(){
      var inputnums = parseInt($("#inputnums").val());
      for(var i=1; i < inputnums + 1; i++){
        var inputmems =
        '<div class="form-row">'+
           '<div class="form-group col-md-6">'+
            '<label for="inputmems">Enter the number of your input' + i + ' '  + 'membership functions:</label>'+
            '<input type="text" class="form-control" id="inputmems'+ i + ' ' + 'name="inputmems'+ i + ' ' + ' placeholder="Enter inputmems">'+
          '</div>'+
        '</div>';
        $("#1").after(inputmems);
      }
    });
  });

when i click on my registerNums button it append my form but i get the wrong syntax in my html code using inspect element:

<input class="form-control" id="inputmems1 name=" inputmems1="" placeholder="Enter inputmems" type="text">

that if you see after my id it extends name in it and other problems

how can i fix this?

4 Answers 4

1

Ok. there is some syntax issue while you create input string. It has to be like this

'<input type="text" class="form-control" id="inputmems'+ i + '" name="inputmems'+ i +  '" placeholder="Enter inputmems">'+

Just change this line in your script

Working fiddle

Sign up to request clarification or add additional context in comments.

Comments

0

You should put the double quotes.

$(document).ready(function() {
  $("#registerNums").click(function() {
    var inputnums = parseInt($("#inputnums").val());
    for (var i = 1; i < inputnums + 1; i++) {
      var inputmems =
        '<div class="form-row">' +
        '<div class="form-group col-md-6">' +
        '<label for="inputmems">Enter the number of your input' + i + ' ' + 'membership functions:</label>' +
        '<input type="text" class="form-control" id="inputmems"' + i + ' ' + 'name="inputmems"' + i + ' ' + ' placeholder="Enter inputmems">' +
        '</div>' +
        '</div>';
      $("#1").after(inputmems);
    }
  });
});

Comments

0
    $(document).ready(function(){
    $("#registerNums").click(function(){
      var inputnums = parseInt($("#inputnums").val());
      var inputmems = "";
      for(var i=1; i < inputnums + 1; i++){
        inputmems +=
        '<div class="form-row">'+
           '<div class="form-group col-md-6">'+
            '<label for="inputmems">Enter the number of your input' + i + ' '  + 'membership functions:</label>'+
            '<input type="text" class="form-control" id="inputmems'+ i + ' ' + 'name="inputmems'+ i + ' ' + ' placeholder="Enter inputmems">'+
          '</div>'+
        '</div>';
      }
      $("#1").append(inputmems);
    });
  });

please use var inputmems and append outside of for loop.

Comments

0

It looks like there is a problem with the html string that you're preparing. You can use Template literals which can make your life easier while preparing a html string with parameters.

var inputmems = `<div class="form-row"><div class="form-group col-md-6"><label for="inputmems">Enter the number of your input${i} membership functions:</label><input type="text" class="form-control" id="inputmems${i}" name="inputmems${i}" placeholder="Enter inputmems"></div></div>`;

Comments

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.