最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can i click on a button when it doesn't have an id - Stack Overflow

programmeradmin0浏览0评论

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

Share Improve this question edited Oct 11, 2012 at 3:02 okm 23.9k5 gold badges85 silver badges91 bronze badges asked Oct 10, 2012 at 22:36 Mechiche-Alami SaadMechiche-Alami Saad 672 gold badges3 silver badges7 bronze badges 2
  • 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 the name attribute. – pedrofurla Commented Oct 11, 2012 at 2:27
Add a ment  | 

7 Answers 7

Reset to default 5

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

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.

发布评论

评论列表(0)

  1. 暂无评论