0

I have a markup something similar to below -

<div class="nav" id="nav">
    <ul>
        <li><a href="">------</a></li>
        <li><a href="">------</a></li>
        <li><a href="">------</a></li>
        <li>
            <a class="mainMenuLink" href="index.php?p=account-information"><i class="fa fa-cog fa-3x"></i> Account Information<span class="fa arrow"></span></a>
        </li>
        <li>
            <a class="active-menu" href="index.php?p=account-function"><i class="fa fa-cog fa-3x"></i> Account Functions<span class="fa arrow"></span></a>
            <ul class='nav nav-second-level in'>
                <li>
                    <a class="subMenuLink" href="index.php?p=create-account">Create a New Account</a>
                </li>
                <li>
                    <a class="active" href="index.php?p=modify-account-select">Modify an Account</a>
                </li>
                <li>
                    <a class="subMenuLink" href="index.php?p=">Password Modification</a>
                </li>
            </ul>
        </li>
  </ul>        
</div>

In this markup I want to select tags that contains 'Account Fuction' and 'Modify an Account'.

This is what I want to select from about Markup -

<a class="active-menu" href="index.php?p=account-function"><i class="fa fa-cog fa-3x"></i> Account Functions<span class="fa arrow"></span></a>

<a class="active" href="index.php?p=modify-account-select">Modify an Account</a>

Can somebody tell me what is the best way to select these elements in jquery?

Hope somebody may help me out. Thank you.

user3733831
  • 2,886
  • 9
  • 36
  • 68

3 Answers3

3

You can just do this:

$("a:contains('Account Functions')");
$("a:contains('Modify an Account')");

This way it will find the elements no matter where they are. jQuery is nice isn't it?

Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54
  • Yes It is.... Can we use this for tag? like `$("th:contains('Emails')")` etc – user3733831 Jan 15 '15 at 02:40
  • @user3733831 Certainly. Just make sure it's in a table. – Spencer Wieczorek Jan 15 '15 at 02:43
  • thank you. One thing now I want to check these elements have a particular class if not I want to add a class. I tried it something like this - – user3733831 Jan 15 '15 at 02:47
  • `$("a:contains('my link')").hasClass("custom").removeClass("custom").addClass("custom");` – user3733831 Jan 15 '15 at 02:47
  • @user3733831 Here is a fiddle with a [way to do that](http://jsfiddle.net/pgdf2kkc/1/). Basically we go through our element(s), check if it has `.custom`, if it does not then add it. Looks like you have the same idea :) – Spencer Wieczorek Jan 15 '15 at 03:01
  • @user3733831 No you don't need it. Although if it's the same class your are adding that you are checking all you need to do is: `$("a:contains('Modify an Account')").addClass( "active" )`. jQuery will not add a duplicate class (it will check for you), so you will not need to worry about that. – Spencer Wieczorek Jan 15 '15 at 03:19
0
 $("#nav").children("ul li").find(".yourelementclass").text();

You can do something like that.

user254153
  • 1,855
  • 4
  • 41
  • 84
0

You can try this code $(".active-menu, .active") You can select class active-menu or class active in one statement

Dat Nguyen
  • 1,881
  • 17
  • 36