John Java
John Java

Reputation: 153

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?

Upvotes: 8

Views: 25275

Answers (2)

rajesh kakawat
rajesh kakawat

Reputation: 10896

try something like this

$(document).ready(function(){
    //javascript
    if(document.getElementById('save')){
        //your code goes here
    }
    
    //jquery
    if($('#save').length){
        //your code goes here
    }
});

Upvotes: 19

palaѕн
palaѕн

Reputation: 73906

You can do this:

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

}

Upvotes: 4

Related Questions