The Learner
The Learner

Reputation: 3927

jquery adding data to a div dynamically

<div class="sample">
</div>

I would like to add data to the sample div on click. I am able to get the data to this function. verified with alert but it is not appending to the sample div.

$("#editsubmit").click(function(){

                  var val = $('[name=name111]').val();
                    var newHTML = '<input type="checkbox" name="'+val+'<br>';
                    $("#sample").append( newHTML );

           });  

Upvotes: 4

Views: 23324

Answers (4)

Giovanni Giordano
Giovanni Giordano

Reputation: 37

$("#sample") 

is used for id

$(".sample")

is used for class

Upvotes: 0

sbru
sbru

Reputation: 887

You used

 $("#sample").append( newHTML );

but you defined sample to be a class (not an id) so you should use

     $(".sample").append(newHTML);

To summarize, the "." is used for class and the "#" is used for id.

Upvotes: 1

user2289659
user2289659

Reputation:

sample is not id in your code its a class so change it to .sample instead of #sample.

Upvotes: 2

Suresh Atta
Suresh Atta

Reputation: 122026

Class and id attributes mismatch.you have given as class and selecting as ID

 $(".sample").append( newHTML );

or change your div to

<div id="sample">  and then  $("#sample").append( newHTML );

Upvotes: 6

Related Questions