Huip
Huip

Reputation: 13

find html element, without proper class name or id, with javascript

I have this HTML code

<div class="option-wrapper">
    <label class="">
        <input type="checkbox" name="name" value="1" data-type-xml="select" aria-label="Untitled">
        <span lang="" class="option-label active">1</span>
    </label>
    <label class="">
        <input type="checkbox" name="name" value="2" data-type-xml="select" aria-label="Untitled">
        <span lang="" class="option-label active">2</span>
    </label>
</div>

im trying to find the labels and to set a "style="display: none;" like this <label class="" style="display: none;>

my problem is, I only need one of the labels in this div. But I don't have a class name or id to find one label. Do u guys know a solution for this?

Upvotes: 1

Views: 63

Answers (1)

marks
marks

Reputation: 1391

Use document.getElementsByTagName('label') instead for all end then get them with the array offset. Or - to select the first - use document.getElementByTagName('label'). Notice the singular version of the second selector.

Or, a more general selector which you can use with either classes or tags or any other query string would be document.querySelector('label') or document.querySelectorAll('label')

Upvotes: 2

Related Questions