In modern browsers (and IE8), you can use document.querySelector
to use any CSS selector to find an element.
In your case, for instance, you could use
var x = document.querySelector('[action="form_action.asp"]');
…to look it up by the value of its action
attribute. The rest of your function doesn’t change.
querySelector
finds the first matching element. If you want a list of all matching elements, you can use querySelectorAll
. In your case, for instance, if you want a list of the input
elements inside the form, you could do this:
var elements = document.querySelectorAll('[action="form_action.asp"] input');
e.g.:
function myFunction() {
var elements = document.querySelectorAll('[action="form_action.asp"] input');
var text = "";
var i;
for (i = 0; i < elements.length ;i++) {
text += elements[i].value + "<br>";
}
document.getElementById("demo").innerHTML = text;
}
3
solved How to get the