user4644565
user4644565

Reputation:

javascript click a button using a script

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

Upvotes: 0

Views: 74

Answers (3)

StormBeast
StormBeast

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

dazzafact
dazzafact

Reputation: 2860

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

Upvotes: 0

AmmarCSE
AmmarCSE

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

Upvotes: 2

Related Questions