Arr
Arr

Reputation: 33

Get elements by class name in a certain div through Javascript

I have:

<div class="f0">
    <div class="input-text">
        <t>Text</t>
    </div>
</div>
var h = 0;
    for(h;h<inputstext.length;h++){
        var str = 'f' + h;
        var currentDiv = document.getElementById(str);


    }

How do I get only the input-text elements value that is in f0?

Output should be Text.

Thanks.

Upvotes: 0

Views: 1952

Answers (1)

Lewis
Lewis

Reputation: 4595

You can use the element[property*="val"] to select all elements with property beginning with "val" - in this case, any class that starts with "f", and then select their .input-text children.

Also, you're trying to get elements with id f0, when these divs are marked with class f0.

const inputs = document.querySelectorAll('[class*="f"] .input-text');
for (const element of inputs)
  console.log(element.textContent.trim());
<div class="f0">
    <div class="input-text">
        inside f0
    </div>
</div>
<div class="f1">
    <div class="input-text">
        inside f1
    </div>
</div>

Upvotes: 2

Related Questions