Mechiche-Alami Saad
Mechiche-Alami Saad

Reputation: 67

How can i click on a button when it doesn't have an id

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">

Upvotes: 2

Views: 2012

Answers (7)

Alcides Queiroz
Alcides Queiroz

Reputation: 9576

With 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()

Upvotes: 5

enhzflep
enhzflep

Reputation: 13099

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.

Upvotes: 0

Jim W
Jim W

Reputation: 5016

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
}

Upvotes: 0

rid
rid

Reputation: 63542

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;
    }
}

Upvotes: 1

Mitch Dart
Mitch Dart

Reputation: 1369

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.com

Upvotes: -1

Ash Burlaczenko
Ash Burlaczenko

Reputation: 25465

If you can use jQuery you could do

$('input[value="Envoi"]').click();

Upvotes: 3

Wilk
Wilk

Reputation: 8113

You can use document.getElementsByName() to retrieve the button.

var button = document.getElementsByName('Envoi');
button.click ();

Here's an example

Upvotes: -1

Related Questions