admin管理员组

文章数量:1336684

This is my second question about clicking buttons in JavaScript but this one I'm really stuk on: I'm trying to click an image button with javascript however there's not much to go on as the source only gives this information about the image button

<div id="claimBtnDv" style="bottom:-30px; position:absolute; right:0; margin-top:0;">
    <input type="image" class="btnClaim" src="websitebuttonimage" onclick="alert('buttonclicked');">
</div>  

I tried to do document.getElementById('claimBtnDv').click() but no success , are there other methods to use?

This is my second question about clicking buttons in JavaScript but this one I'm really stuk on: I'm trying to click an image button with javascript however there's not much to go on as the source only gives this information about the image button

<div id="claimBtnDv" style="bottom:-30px; position:absolute; right:0; margin-top:0;">
    <input type="image" class="btnClaim" src="websitebuttonimage" onclick="alert('buttonclicked');">
</div>  

I tried to do document.getElementById('claimBtnDv').click() but no success , are there other methods to use?

Share Improve this question edited Jun 21, 2012 at 22:11 Derek 朕會功夫 94.4k45 gold badges197 silver badges253 bronze badges asked Jun 21, 2012 at 21:56 NubcakeNubcake 3972 gold badges7 silver badges18 bronze badges 2
  • 1 it works for me - jsfiddle/wMRbn – Zoltan Toth Commented Jun 21, 2012 at 21:59
  • You get the message 'button clicked' when you use document.getElementById('claimBtnDv').click() ? – Nubcake Commented Jun 21, 2012 at 22:00
Add a ment  | 

3 Answers 3

Reset to default 3

If you can't change the HTML and you need to click the image using javascript, you can go into the child nodes to find the image. This is pretty easy since a class name is provided.

var nodes = document.getElementById('claimBtnDv').childNodes;

for ( i = 0; i < nodes.length; i++ ) {
    if ( nodes[i].className === "btnClaim" ) {
        console.log("clicking!");
        nodes[i].click();
        break;
    }
}

Your code almost works - just change your class="btnClaim" to id="btnClaim" - and the getElementById will work - http://jsfiddle/wMRbn/1/

document.getElementById('btnClaim').onclick = function () {
    alert ( "click" );
}​

UPDATE To get an element without an id you can do this - get the parent element by id and attach an event listener to its' input child - http://jsfiddle/wMRbn/3/

var img = document.getElementById("claimBtnDv").getElementsByTagName("input")[0];
img.onclick = function () {
    alert ( "click" );
}​

it looks like it works in that case. Are you trying to assign it different click events or something?

jquery would allow you to do something like:

$("input.btnClaim").click(function(){
  //in here can go a lot more functions, etc.
});

javascript would do (after you give it an id of 'btnClaim'):

var X = document.getElementById("btnClaim");
x.onclick = "JAVASCRIPT GOES HERE";  
//from what the wc3 doc looks like, you need to have it in quotes, 
//   but i may be mistaken.

Maybe that helps ?

本文标签: JavaScript Click Image ButtonStack Overflow