0

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?

2 Answers 2

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

2 Comments

Thanks that helped. :)
@MutasimFuad, you're welcome. I'm glad it worked. Please don't forget to accept the answer if it helped. Thanks.
1

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

This is not ok. Hint: name attribute is closing earlier.

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.