简体   繁体   中英

toggle hidden label for element

How can I check if a element has a label and call the toggle function to display it?

Trying this:

if($('label').attr('for') == $(this).attr('name')) {
    alert($('label').attr('for') == $(this).attr('name')); // displays true if hidden or displayed
    $('label').attr('for='+$(this).attr('name')).toggle(true); // error
}

HTML not hidden (this is just to show the correct syntax as the element is hidden):

<div>
    <label for="state" class="ui-select">
        State*
    </label>
    <div class="ui-select">
        <a href="#" role="button" aria-haspopup="true" data-theme="z" class="ui-btn ui-btn-icon-right ui-btn-corner-all ui-shadow ui-btn-down-z ui-btn-up-z">
        <span class="ui-btn-inner ui-btn-corner-all">
            <span class="ui-btn-text">State*</span>
            <span class="ui-icon ui-icon-arrow-d ui-icon-shadow"></span>
        </span>
        </a>
        <select name="state" id="state" tabindex="-1" class="required">
            <option value="">State*</option>
            <option value="AK">ALASKA</option>
            <option value="...">...</option>
        </select>
    </div>
</div>

HTML hidden:

<div>       
    <div class="ui-select">
        <a href="#" role="button" aria-haspopup="true" data-theme="z" class="ui-btn ui-btn-icon-right ui-btn-corner-all ui-shadow ui-btn-down-z ui-btn-up-z">
        <span class="ui-btn-inner ui-btn-corner-all">
            <span class="ui-btn-text">State*</span>
            <span class="ui-icon ui-icon-arrow-d ui-icon-shadow"></span>
        </span>
        </a>
        <select name="state" id="state" tabindex="-1" class="required">
            <option value="">State*</option>
            <option value="AK">ALASKA</option>
            <option value="...">...</option>
        </select>
    </div>
</div>

It sounds like you're looking for $('label[for="' + this.name + '"]') .
You can use the Attribute Equals selector to select elements with a specific attribute value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM