Checking for a button

I have a .js file that is called by several .jsp pages. What I need to do in this particular .js file is to call a specific function only if the "Save" button is present on the .jsp page. How to check if a button exists? Currently, in the .js file, the button refers to this method: $ ('Button [ID = save]')

How to check if such a button exists? Hope someone can advise. Thanks.

+6
source share
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 } }); 
+12
source

You can do it:

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

Source: https://habr.com/ru/post/958366/


All Articles