Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if specific id is called from jQuery?

I have a MainContent div which contains main content of the website which could be loaded from ajax.

how can i find out if $("#MainContent").load("someUrl") was called, so i am able to push new history state to the webbrowser?

like image 619
Timsen Avatar asked Apr 18 '15 08:04

Timsen


People also ask

How do I know which ID is clicked in jQuery?

Answer: Use the jQuery attr() Method You can simply use the jQuery attr() method to get or set the ID attribute value of an element. The following example will display the ID of the DIV element in an alert box on button click.

How do you check if the ID exists in Javascript?

getElementById() to get the ID and store the ID into a variable. Then use JSON. stringify() method on the element (variable that store ID) and compare the element with 'null' string and then identify whether the element exists or not.

What does the jQuery .wrap function do?

wrap( function ) A callback function returning the HTML content or jQuery object to wrap around the matched elements. Receives the index position of the element in the set as an argument. Within the function, this refers to the current element in the set.

How check Div is available or not in jQuery?

length){ alert('Found with Length'); } if ($('#DivID'). length > 0 ) { alert('Found with Length bigger then Zero'); } if ($('#DivID') != null ) { alert('Found with Not Null'); } }); Which one of the 3 is the correct way to check if the div exists?


1 Answers

Like LSletty said, if you want to know when it is called use the handler from .load() itself:

$("#MainContent").load("path/content.html", function(){
      // Do stuff when load is called ...
});

More info here: jQuery load event

To take action after completion I would use the .done() handler.

Use it in the following way:

$("#MainContent").load("path/content.html").done(function(){
  // Do stuff when load is done ...
});

From jQuery docs:

Add handlers to be called when the Deferred object is resolved.

More info here: deferred.done() jQuery

like image 95
Mathieu Magalhaes Avatar answered Nov 10 '22 17:11

Mathieu Magalhaes