MB34
MB34

Reputation: 4434

jQuery->get id from other attribute

In the HTML below, I need to retrieve the p_id attribute based on the p_code attribute. For example:

If I have p_code of "WH15", I need "63" returned.

how would I do that using jQuery?

<div id="deslist_13" class="sch_accordion_item_content_active" style="display: block;">
    <div class="left_shadow"></div>
    <div p_code="BRE" id="pdiv_60" p_id="60" class="program odd">
      <input type="hidden" id="hidden_60" value="BRE">
      <input type="hidden" id="tabId_60" value="13">
      <div class="cb"></div><span>BRE</span>
    </div>
    <div p_code="WH13" id="pdiv_61" p_id="61" class="program">
      <input type="hidden" id="hidden_61" value="WH13">
      <input type="hidden" id="tabId_61" value="13">
      <div class="cb"></div><span>WH13</span>
    </div>
    <div p_code="WH14" id="pdiv_62" p_id="62" class="program odd">
      <input type="hidden" id="hidden_62" value="WH14">
      <input type="hidden" id="tabId_62" value="13">
      <div class="cb"></div><span>WH14</span>
    </div>
    <div p_code="WH15" id="pdiv_63" p_id="63" class="program">
      <input type="hidden" id="hidden_63" value="WH15">
      <input type="hidden" id="tabId_63" value="13">
      <div class="cb"></div><span>WH15</span>
    </div>
    <div p_code="BPA" id="pdiv_64" p_id="64" class="program odd">
      <input type="hidden" id="hidden_64" value="BPA">
      <input type="hidden" id="tabId_64" value="13">
      <div class="cb"></div><span>BPA</span>
    </div>
    <div p_code="BPR" id="pdiv_65" p_id="65" class="program">
      <input type="hidden" id="hidden_65" value="BPR">
      <input type="hidden" id="tabId_65" value="13">
      <div class="cb"></div><span>BPR</span>
    </div>
</div>

Upvotes: 0

Views: 241

Answers (1)

Matt Ball
Matt Ball

Reputation: 360016

var p_id = $('div[p_code="WH15"]').attr('p_id');

Demo: http://jsfiddle.net/mattball/9Htdq/


However: it would be far better to use HTML5 data-* attributes if you can alter the markup. They are allowed by the HTML5 spec, and work automagically with .data().

Upvotes: 3

Related Questions