views:

77

answers:

4

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

A: 
$('button span').each ( function() {
    if($(this).text() === "Refund Offline" )
    {
        $(this).parent().addClass ( 'hide_button' );
    }
});
rahul
+5  A: 
$("button > span:contains('Refund Offline')").parent().addClass("hide_button");
jitter
Is this an exact match for the string 'Refund Offline'?
rahul
No it isn't. It matches all spans which: are child of a button and contain the string 'Refund Offline'
jitter
You could save a function call by using the `$("button:has(> span:contains('Refund Offline'))").addClass('hide_button');` But who knows how many extra calls that makes behind the scenes.
Doug Neiner
A: 

Try this,

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

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

NilColor
It seems to me that the first selector is going to look at every single node on the page. Is that correct? If so.....
Justin Johnson
Yes. It look for every node and check for text. You can narrow down it by adding something like `"button :contains"`... Main idea was to say that if you are not sure a text is inside button>span - you can search for more node this way. Of course you should try to write more specific selector. If you can.
NilColor