Dan
Dan

Reputation: 1295

getting element by attribute jquery

is there any way to get an element in the page by attribute

so lets say i have this HTML

<ul id="tabMenu">
  <li class="active"><a href="#home-loans">Home Loan</a></li>
  <li><a href="#savings">Savings</a></li>
  <li><a href="#car-loan">Car Loan</a></li>
  <li><a href="#credit-card">Credit Card</a></li>
</ul>

<div data-alias="#credit-card" class="tabContent">Content 1...</div>
<div data-alias="#savings" class="tabContent">Content 2...</div>
<div data-alias="#car-loan" class="tabContent">Content 3...</div>
<div data-alias="#credit-card" class="tabContent">Content 4...</div>

and this jQuery

jQuery("#tabMenu li").each(function() {

tabid = jQuery(this).find("a").attr("href");

    if (jQuery(".tabContent").attr("data-alias") == tabid) {

       $tabHTML = //get div element that is for this tab
    }

});

as can be seen from the comment in the code I am unsure of how to get the element. so for example for the li with the href of #car-loan get the div with the data-alias of #car-loan

thanks

Upvotes: 3

Views: 8195

Answers (3)

Matt Busche
Matt Busche

Reputation: 14333

you would want to use

$tabHTML = $( "div[ data-alias='" + tabid + "']" );

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

you mean:

$tabHTML = $("div[data-alias='"+tabid +"']");

Upvotes: 1

Joseph Silber
Joseph Silber

Reputation: 219920

Use a regular attribute selector:

$tabHTML = jQuery(".tabContent[data-alias='" + tabid + "']");

Upvotes: 2

Related Questions