Arnav Nath
Arnav Nath

Reputation: 81

how to make a button pressed by default in html or javascript

There I have a button that is hidden from the user but want it to be clicked by default like with a check box if you want it to be checked by default you add the checked attribute is there any way you could do the same thing with a button here is my code

<input id="submit" type="hidden" value="Reverse Geocode" autofocus> 

Upvotes: 5

Views: 13928

Answers (5)

astrosixer
astrosixer

Reputation: 121

Trigger click event on the button as soon as document is ready.You have to write the click event as shown below.

$(document).ready(function(){

    $("#yourButtonId")[0].click();

});  

Upvotes: 2

Mamun
Mamun

Reputation: 68923

May be you can do the following:

document.getElementById('chkTest').addEventListener('click', function(){
  if(this.checked)
    document.getElementById('submit').click();
});

document.getElementById('submit').addEventListener('click', function(){
  alert('button clicked');
});
<input id="submit" type="hidden" value="Reverse Geocode" autofocus />

<input type="checkbox" id="chkTest" /> Check To Click The Button

Upvotes: 3

CodeF0x
CodeF0x

Reputation: 2682

At first, your button is not a button. It's a a hidden field.

In order to make it a button, change type="hidden" to type="button". To make it invisible to the user, you could use inline styles like this: style="display: none;".

As a result, your button looks like this: <input id="submit" style="display: none" type="button" value="Reverse Geocode">


Now, to click it, simply call the click() method:

document.getElementById('submit').click();

Upvotes: 2

Siva Ganesh
Siva Ganesh

Reputation: 1445

Now i understand your question, You want default click in your submit button. Try click event, It will trigger the submit.

<script>
   $('#submit').trigger('click');
</script>

In JavaScript

   document.getElementById("submit").click();

Upvotes: 1

Tiago Peres
Tiago Peres

Reputation: 15476

You can do as following:

<script type="text/javascript">
document.getElementById("submit").click();
</script>

Upvotes: 7

Related Questions