3

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

3 Answers 3

2

Use a regular attribute selector:

$tabHTML = jQuery(".tabContent[data-alias='" + tabid + "']");
Sign up to request clarification or add additional context in comments.

Comments

1

you mean:

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

Comments

1

you would want to use

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.