Reputation:
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> Attend</a></li>
echo "<script>
document.getElementById('attendBtn').click();
</script>";
but it does not seem to work. any help would be appreciated
Upvotes: 0
Views: 74
Reputation: 86
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'?
Upvotes: 0
Reputation: 30557
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> 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
Upvotes: 2