I am trying to add some DOM when click in a button. But i want to add the name attribute dynamically. Here is my code
$(document).ready(function() {
console.log("done");
var counterEdu = 1;
$("#add-edu").click(function (event) {
event.preventDefault();
counterEdu = counterEdu+1;
$("#passing-year").after('<div class="col-md-12 form-group1">'+'<input type="text" placeholder="Degree Name" name="degree"+counterEdu;>'
});
});
I want to add the name attribute concatenated with
counterEdu
variable. like degree1,degree2..... so how to do that?
asked Mar 5, 2017 at 0:23
Mutasim Fuad
6063 gold badges13 silver badges32 bronze badges
2 Answers 2
You need to escape it properly:
$("#passing-year").after('<div class="col-md-12 form-group1">' + '<input type="text" placeholder="Degree Name" name="degree'+counterEdu +'">'
answered Mar 5, 2017 at 0:27
Ionut Necula
11.6k4 gold badges49 silver badges72 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Mutasim Fuad
Thanks that helped. :)
Ionut Necula
@MutasimFuad, you're welcome. I'm glad it worked. Please don't forget to accept the answer if it helped. Thanks.
Since counterEdu is not a string
$(document).ready(function() {
console.log("done");
var counterEdu = 1;
$("#add-edu").click(function (event) {
event.preventDefault();
counterEdu = counterEdu+1;
$("#passing-year").after('<div class="col-md-12 form-group1">'+'<input type="text" placeholder="Degree Name" name="degree'+counterEdu+'">'
});
});
1 Comment
Ionut Necula
This is not ok. Hint:
name attribute is closing earlier.lang-js