Hamid Sarfraz
Hamid Sarfraz

Reputation: 1135

js/jquery not working with ajax data

I have some js and jquery on page (in external file), and everything is working fine on the pageload. All the effects are working fine. The problem starts when i submit form through jquery and get more js data from the server and append it to the body through jquery. The data itself is jquery and adds HTML to a specified element. Now, anything inside that piece of HTML doesnt trigger previous jquery code. I have to resend the same code again and again with each ajax response. Is this a problem on normal behaviour of the language.

Im a beginner.

Herz the code that works fine before, but doesnt afterwords:

$('form').submit( function() {
            switch( $(this).attr('name') )
            {
                case 'ajaxeditbookmarkform':
                alert( '1' );
                case 'ajaxbookmarkform':
                alert( '2' );
            }
            return false;
    });

Upvotes: 0

Views: 130

Answers (3)

AndersDaniel
AndersDaniel

Reputation: 1162

First off. Yes this is normal behavior. What you could try to do is to use the .live function from jQuery like so:

$('form').live('submit', function() {
        switch( $(this).attr('name') )
        {
            case 'ajaxeditbookmarkform':
            alert( '1' );
            case 'ajaxbookmarkform':
            alert( '2' );
        }
        return false;
});

Upvotes: 1

Rupesh Pawar
Rupesh Pawar

Reputation: 1917

try live events.....

This method provides a means to attach delegated event handlers to the document element of a page, which simplifies the use of event handlers when content is dynamically added to a page. See the discussion of direct versus delegated events in the .on() method for more information.

Upvotes: 2

Akhil Thayyil
Akhil Thayyil

Reputation: 9403

You missed break statement in case ...

check the fiddle

Upvotes: 2

Related Questions