How to find element id when i only know text inside element using jQuery?

I have a form with id = "main-form" and inside there is class = "block footer"

Inside this element:

<button type="button">Submit</button>

How can I use jQuery to find this button without adding an identifier to an element? The button always has the text Send.

Is it possible to use id = "main-form" as a starting point in my choice to make it more efficient?

+3
source share
7 answers

Select all the elements <button>, then filter them based on innerHTML:

$('button').filter(function() {
  return this.innerHTML == 'Submit';
});

If necessary, you can be more explicit only by selecting the button elements with type="button", although this is slightly less efficient:

$('button[type="button"]').filter(function() {
  return this.innerHTML == 'Submit';
});

:

, , div , , ?

, .

var $container = $('.element-that-contains-the-button');
$('button', $container).filter(function() {
  return this.innerHTML == 'Submit';
});

$container.find(), .

+3

, "".

$('button:contains("Submit")')...

, , :

$('.block-footer button:contains("Submit")')...
+1

jQuery('#main-form button:contains("submit")') submit mainform, .

0

jquery .

var yourButton = $('button:Contains("Submit")');
0

: .

:

$(":contains('Submit')").Action()
0

JQuery ( ''). (() {  JQuery ( ''). ('button'). addClass ( '') });

0

Since you already know the form identifier, you just need to loop over each button element and check if it contains the submit text.

    $(document).ready(function(){
        $("#main-form button").each(function(e){
             var btnCaption = $(this).text();
            if(btnCaption == 'Submit'){
                //do something
                alert('hey, i am the submit button');
            }
        });

    });

I have included a working example DEMO

0
source

All Articles