+ 1
in this example why use the value wordđ€đ€and what meaning it?
function change() { var x = document.getElementById('name'); x.value = x.value.toUpperCase(); }
5 Answers
+ 4
Your example code supposed an <input> html element like:
<input type="text" id="name" value="42">
The "type" attribut could be another, the 'id' provide the 'name' used in your code, so 'x' contain a reference to the <input> object, and the 'value' can be ommitted: if not set, the field will be empty by default ( at first page loading ), else this is the value used for prefill the field by default. At runtime, the 'value' attribute can be accessed ( read/write ) dynamically with JS as an attribute of the <input> object...
Anyway, your example code can be trivial, as common way to use <input>s in <form> element is to provide a 'name' attribute to each field ( with oe without an id ) to be used with submitted data:
<form id="test">
<input name="name" ...>
<input name="firstname" ...>
<input name="phone" ...>
</form>
... so, id are not necessary, as you can select element by name ( but don't forgot that contrarly to select by id, you will get a list of element ):
var x = document.getElementsByName("phone")[0];
Or even:
var e = document.getElementById("test");
var x = e.getElementsByName("phone")[0];
Another way to select an element, are the powerfull window.querySelector() and window.querySelectorAll() ( one return the first encountered, the other a list of all corresponding to the css selector passed at argument to the method ):
var x = window.querySelector("#test input[name=phone]);
+ 2
value is the attribute of an input element
0
The user object contains an "id" and a "name" property.
Given an array of user objects called "arr", create a list of names, where each element's key is represented by the id.
Answer:
const users = arr.map((user) =>
<li key={user.id}>
{user.name}
</li>);
0
Fill in the blanks to create a functional component Search, which sets the initial value of the text field in the state using the received props.
Answer:
function Search(props) {
const [val, setVal] = useState(props.q);
return <div>
<input type="text" value={val} />
<button>Search</button>
</div>;
}
- 1
really thank you to all