user213634
user213634

Reputation:

how to find buttons with specified text inside using jquery?

I have this code structure , and i want to search for span that has text "Refund Offline" and then add the class hide_button to the parent tag " button".

basically I want to hide the button that has "Refund Offline" text.

<button class="scalable save submit-button" type="button" id="id_b5295d98b1d6eb3012e2dfd801ede120">

<span>Refund Offline</span>

</button>

Using jQuery

thanks in advance

Upvotes: 23

Views: 35207

Answers (4)

NilColor
NilColor

Reputation: 3532

If your text isn't in a span that is a child of button (or you are not 100% sure it is) use

$(":contains('Refund Offline')").closest('button').addClass("hide_button");

.closest will return closest button element

Upvotes: 15

jitter
jitter

Reputation: 54605

$("button > span:contains('Refund Offline')").parent().addClass("hide_button");

Upvotes: 24

theraneman
theraneman

Reputation: 1630

Try this,

$('button span:contains("Refund Offline")').parent().addClass("hide_button");

Upvotes: 3

rahul
rahul

Reputation: 187030

$('button span').each ( function() {
    if($(this).text() === "Refund Offline" )
    {
        $(this).parent().addClass ( 'hide_button' );
    }
});

Upvotes: 1

Related Questions