0

I would like to click a button using javascript without physically clicking on the bottom.

Below is what I have tried:

<li id="#attendBtn"><a style="background:#FF6400;" href="#attend"><i class="fa fa-check"></i>&nbsp;&nbsp;Attend</a></li>



echo "<script>            
document.getElementById('attendBtn').click();
</script>";

but it does not seem to work. any help would be appreciated

1
  • The ID is on the <li> tag, so you are trying to click the <li> tag, not the <a> tag. Could that be your issue? Commented May 31, 2015 at 12:43

3 Answers 3

2

Remove the # in <li id="#attendBtn">.

document.getElementById('attendBtn').click();
<li id="attendBtn" onclick="console.log(1);"><a style="background:#FF6400;" href="#attend"><i class="fa fa-check"></i>&nbsp;&nbsp;Attend</a></li>

In javascript, if you use getElementById, you only need to pass the id name. Other frameworks such as jQuery use the # as an alias to id

Sign up to request clarification or add additional context in comments.

Comments

0
 document.getElementById('attendBtn').onclick();

Comments

0

If I understand your question correctly, you want to trigger the click event without actually having to click on the button? You should look into using jQuery for that then.

Then you can do this:

$('#attendBtn').click(function() {
    console.log('button was clicked');
});

And trigger it with this:

$('#attendBtn').trigger('click');

Also, I'm guessing the rest of the html is not posted, as an 'li' should be in a 'ul'?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.