Thunder69
Thunder69

Reputation: 3

I can't use a form I added with append

I need to append a form (with a submit input). The problem is the submit input doesn't work after I added it...

Thank you in advance !

Here is when I add it :
$('div.message_holder').append('<div class="mesMessages">' + 
'<b style="color: #000">' + 
msg.username + '</b><br>' + msg.message +
'<form action="" method="POST" class="like">' +
'<input type="submit" id="like" value="Like"/>' + 
'</form>' + '</div>')

And here when I use it :
$( 'form.like' ).on( 'submit', function( e ) {
e.preventDefault()
socket.emit( 'likeEmission', {
username : "Joe",
like : "1"
})

Upvotes: 0

Views: 51

Answers (2)

wasanga7
wasanga7

Reputation: 242

The JQuery method should be:

$('.like').submit(function);

Upvotes: 0

Dev Man
Dev Man

Reputation: 2137

Your submit event is at the element level, set it to the document level

Change

$( 'form.like' ).on( 'submit', function(){})

To

$(document).on( 'submit', "form.like", function(){})

Additionally wrap the code in a document.ready() if you haven't already done so

Or another solution is to use the .bind() method of jQuery See ref: http://api.jquery.com/bind/

Upvotes: 1

Related Questions