Reputation: 55
I want to create a dynamic form using jQuery
$("#AddMobile").click(function () {
var newTextBoxDiv = $(document.createElement('div')).attr("id", 'MobileDiv' + counter);
newTextBoxDiv.after().html('<input type="text" id="Mobile' + counter + '"/>' +
'<input type="button" id="DeleteMobile+' + counter + '" class="DeleteMobile" Value="Delete"/>');
newTextBoxDiv.appendTo("#TextBoxesGroup");
counter++;
});
and when I click on the add button I get this error:
"Uncaught TypeError: Cannot call method 'html' of undefined"
from the console
Is it a problem with libraries? How can I know to which library a function belongs?
Upvotes: 0
Views: 3286
Reputation: 597
Just remove the after()
in the line
newTextBoxDiv.after().html('<label>Textbox #'+ counter + ' : </label>' + '<input type="text" name="textbox' + counter + '" id="textbox' + counter + '" value="" >');
Its work properly. Just try it.
Upvotes: 1
Reputation: 33875
A few changes to your code:
You don't need to do this $(document.createElement('div'))
, you can let jQuery do it for you $("<div>")
.
You probably want to use either .after()
or .html()
, not the way you use it now .after().html("...")
. With .after()
you can add the HTML after the element you have selected (in your case the newly created div). With .html()
you modify the content within the selected element.
Upvotes: 0