简体   繁体   中英

How to match a documentElement ID by finding the documentElement Name?

So on a website every time you refresh it the ID of a specific element changes but the name of it stays the same. I want to find the name and then match it with the document ID that was in the same document. I was thinking of running a loop but I dont know how to do it. This is how the html looks:

<input class="input-text" type="text" data-bind="
    value: value,
    valueUpdate: 'keyup',
    hasFocus: focused,
    attr: {
        name: inputName,
        'aria-describedby': getDescriptionId(),
        'aria-required': required,
        'aria-invalid': error() ? true : 'false',
        id: uid,
        disabled: disabled
    }" name="firstname" aria-required="true" aria-invalid="false" id="N6YFA53">

I want to match the name "firstname" with the id and then change the value of it because the id changes each time i refresh the page.

You can use querySelector to based on the input name then just use id .

 var first_name_id = document.querySelector("input[name='firstname']").id; console.log(first_name_id);
 <input class="input-text" type="text" data-bind=" value: value, valueUpdate: 'keyup', hasFocus: focused, attr: { name: inputName, 'aria-describedby': getDescriptionId(), 'aria-required': required, 'aria-invalid': error()? true: 'false', id: uid, disabled: disabled }" name="firstname" aria-required="true" aria-invalid="false" id="N6YFA53">

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