简体   繁体   中英

Using placeholder as the typical id attribute in JavaScript

This question will probably either be extremely someone or flat out impossible. Inside of JavaScript how would I go about changing the value attribute of a input tag in html based on the placeholder value? This question does sound dumb as a normal person would just set each of the divisions with different ids and change it that way. However, I'm creating an extension for a page with JavaScript that doesn't have id values on the input tags. Note: all of the placeholder texts are different, there are pretty much no id parent divisions except the wrapper, all the classes are the same for the input tag I wish to modify.

Thanks in advance. (It's a dumb question I know. Forgive me)

Html I'm editing

<input type=“tel” maxlength=“1” placeholder=“1” value class=“UIInput-input> 

It's the same thing for the rest but each time the place holder goes up by one.

To find elements in the DOM based on attributes other than ID or Class, you can use the document.querySelector() method.

In your particular example, you would use it like this:

var newValue = 'my new value for my input element';
document.querySelector('input[placeholder="1"]').value = newValue;

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