I would like to click on the button that is named Envoi
, but I can't find an id to reference it by using getElementById("Envoi").click
.
Is there any other way to do this?
The website name is : /
and the button name is Envoi
.
And the associated html is:
<input type="button" value="Envoi">
I would like to click on the button that is named Envoi
, but I can't find an id to reference it by using getElementById("Envoi").click
.
Is there any other way to do this?
The website name is : http://famille-frappier.fr/
and the button name is Envoi
.
And the associated html is:
<input type="button" value="Envoi">
- 5 What's keeping you from just adding an ID to the button?? That, in my opinion, would be the most proper way to do what you're after anyway. – Mike Commented Oct 10, 2012 at 22:43
-
You can navigate the DOM something like
document.children[5].children[2]
for a very crude example. JQuery makes it a lot easier. BTW, it's not named 'Envoi` it has value "Envoi", for naming you have to use thename
attribute. – pedrofurla Commented Oct 11, 2012 at 2:27
7 Answers
Reset to default 5With pure Javascript:
I saw your site, you don't use jQuery, so my solution also doesn't use it.
In your case, you have no id or name in your button, you have only the value, so you need to use this:
document.querySelector('input[value="Envoi"]').click()
If you can use jQuery you could do
$('input[value="Envoi"]').click();
To obtain the element, you could use document.getElementsByTagName()
to get all the elements, then loop through them to find the one you're interested in:
var elements = document.getElementsByTagName("input");
var envoi;
for (var i in elements) {
if (elements[i].getAttribute && elements[i].getAttribute("value") == "Envoi") {
envoi = elements[i];
break;
}
}
var els = document.getElementsByTagName("input");
for(var i=0; i<els.length; i++){
if(els[i].value=='Envoi') //do something with els[i], thats your button
}
Well, if you type this into the console, you get the word Envoi displayed.
document.getElementById('nav_554').getElementsByTagName('input')[0].value
Therefore, you can use the expression to attach a click event listener. Or of course, you could just add the onclick attribute to the button itself.
This code typed into the console will attach a function to that button:
document.getElementById('nav_554').getElementsByTagName('input')[0].addEventListener('click', function(){alert("you clicked me");}, false);
No need for jQuery - total overkill for this simple task.
You can use document.getElementsByName()
to retrieve the button.
var button = document.getElementsByName('Envoi');
button.click ();
Here's an example
The easiest way in my opinion is using JQuery.
$('[name="Envoi"]').click();
Just download the JQuery.js and include it in the page and the above statement will work. For more information view http://api.jquery.