Carl Weis
Carl Weis

Reputation: 7042

how to select a li a based on the text using jQuery

How can I select a given li a element so that it is selected by default?

Please refer to this question to see the example that I'm working with...

How can I navigation up and down a ul using two buttons?

Basically I need to select the Patient Test li a item by default.

Upvotes: 4

Views: 6280

Answers (5)

Carl Weis
Carl Weis

Reputation: 7042

I just had to create an ID for each li and then use jQuery addClass and removeClass to swap the classes.

  $("#repeatMenuItem").removeClass("active");
  $("#deleteMenuItem").removeClass("active");
  $("#okMenuItem").addClass("active");  

Upvotes: 0

Jason Gennaro
Jason Gennaro

Reputation: 34855

You could do something like this

$('li a').each(function(){
    if($(this).text() == 'PATIENT TEST'){
    //do something here
    }
});

Example: http://jsfiddle.net/jasongennaro/uuaKH/

Upvotes: 3

Chandu
Chandu

Reputation: 82893

Try this:

$(function(){
 $("#MainMenu li:contains('PATIENT TEST')").click();
});

Upvotes: 4

John Kurlak
John Kurlak

Reputation: 6680

Try:

<script type="text/javascript">
$(function() {
    $('#MainMenu li > a').eq(0).focus()
});
</script>

Upvotes: 1

braindamage
braindamage

Reputation: 2256

set in the loaded html, since the next click will remove and reset the "status", based on the other post

<div id="MainMenu">
    <ul>
        <li class="menuActive"><a href="#">PATIENT TEST</a></li>
        <li><a href="#">QC TEST</a></li>
        <li><a href="#">REVIEW RESULTS</a></li>
        <li><a href="#">OTHER</a></li>
    </ul>
</div>

Upvotes: 0

Related Questions