Getting element by jquery attribute
is there a way to get an element on the page by attribute
so let's 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 you can see from the comment in the code, I'm not sure how to get the item. so for example for li with href # car-loan get div with data alias # car-loan
thank
+3
Dan
source
to share
3 answers
Use a regular attribute selector :
$tabHTML = jQuery(".tabContent[data-alias='" + tabid + "']");
+2
Joseph silber
source
to share
you mean:
$tabHTML = $("div[data-alias='"+tabid +"']");
+1
Sudhir Bastakoti
source
to share
would you like to use
$tabHTML = $( "div[ data-alias='" + tabid + "']" );
+1
Matt busche
source
to share