Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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> 
like image 962
Arnav Nath Avatar asked May 18 '18 05:05

Arnav Nath


People also ask

How do I make a button press in HTML?

The <button> element is used to create an HTML button. Any text appearing between the opening and closing tags will appear as text on the button. No action takes place by default when a button is clicked. Actions must be added to buttons using JavaScript or by associating the button with a form.

How do you press a button in JavaScript?

The click() method simulates a mouse-click on an element. This method can be used to execute a click on an element as if the user manually clicked on it.

How do I keep a button selected in HTML?

You can use this document. getElementById("some_id"). style. border.

What is default button type in HTML?

The default type for Internet Explorer is "button", while in other browsers (and in the W3C specification) it is "submit".


2 Answers

You can do as following:

<script type="text/javascript">
document.getElementById("submit").click();
</script>
like image 119
Tiago Martins Peres Avatar answered Sep 27 '22 18:09

Tiago Martins Peres


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
like image 21
Mamun Avatar answered Sep 27 '22 20:09

Mamun