Mark S
Mark S

Reputation: 3

Jquery select previous element

I am new to Jquery and I've had a look through the replies on here but I can't find any specific answers to my question.

I have a (ASP) generated table a snippet of which is:

<a href="javascript:__doPostBack(&#39;gv2&#39;,&#39;Select$15&#39;)"
style="color:White;">S</a></td><td style="font-size:XX-Small;">1104</td>
<td style="font-size:XX-Small;">320.20.0116.090</td>
<td style="font-size:XX-Small;">*Not Found*</td>

What I'm trying to do is highlight the *Not Found text and then disable the preceeding href so the link cannot be clicked.

I have developed the following selector:-

$('td').highlight('Not Found').each(function(){$(this).prev("a").removeAttr("href")});

The highlight selector works but the removeattr doesn't. Syntax is probably incorrect but any pointers would be very useful.

Answered:- This works

$("td:contains('*Not Found*')").each(function(){$(this).parent().find('a').removeAttr("href")})

Upvotes: 0

Views: 100

Answers (2)

David Thomas
David Thomas

Reputation: 253308

I'd personally suggest:

// selects all 'td' elements
$('td').filter(function(){
    // retains only those whose text is precisely '*Not Found*'
    return $.trim($(this).text()) == '*Not Found*';
// moves to the closest (ancestor) 'tr'
// finds the 'a' element within that 'tr' element
// sets the 'href' attribute to be equal to '#'
}).closest('tr').find('a').attr('href','#');

JS Fiddle demo.

Alternatively, rather than setting the href to #, you could just remove the a element by unwrapping its contents:

$('td').filter(function(){
    return $.trim($(this).text()) == '*Not Found*';
}).closest('tr').find('a').contents().unwrap();

JS Fiddle demo.

References:

Upvotes: 1

mwebber
mwebber

Reputation: 386

try $(this).prev().find("a").removeAttr("href")}

also removing the link might not work.

try replacing the href with #

Upvotes: 0

Related Questions