Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 594
Dan Avatar asked Jun 30 '26 20:06

Dan


2 Answers

Use a regular attribute selector:

$tabHTML = jQuery(".tabContent[data-alias='" + tabid + "']");
like image 165
Joseph Silber Avatar answered Jul 02 '26 12:07

Joseph Silber


you mean:

$tabHTML = $("div[data-alias='"+tabid +"']");
like image 37
Sudhir Bastakoti Avatar answered Jul 02 '26 11:07

Sudhir Bastakoti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!