Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a button exists

I have a .js file that is invoked by a few .jsp pages. What I need to do in this particular .js file is to invoke a certain function only if the 'save' button is present in the .jsp page. How do I check if the button exists? Currently, in the .js file the button is referred to this way: $('button[id=save]')

How do I check if such a button exists?

like image 985
John Java Avatar asked Nov 19 '13 07:11

John Java


People also ask

How do you know if an element is available or not?

To check the presence of an element, we can use the method – findElements. The method findElements returns a list of matching elements. Then, we have to use the method size to get the number of items in the list.

How do you check button is exist or not in jQuery?

In jQuery, you can use the . length property to check if an element exists. if the element exists, the length property will return the total number of the matched elements.

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?


2 Answers

try something like this

$(document).ready(function(){
    //javascript
    if(document.getElementById('save')){
        //your code goes here
    }
    
    //jquery
    if($('#save').length){
        //your code goes here
    }
});
like image 82
rajesh kakawat Avatar answered Sep 19 '22 19:09

rajesh kakawat


You can do this:

if($('#save').length > 0){
    // Button exists
} else {
    // Button is not present in the DOM yet

}
like image 22
palaѕн Avatar answered Sep 18 '22 19:09

palaѕн