5

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> 
5
  • what you want? If user clicked the check box button should be clicked?? Commented May 18, 2018 at 5:09
  • you can just call the click event of the button window.load() or if you have a function assigned to it just call it. Commented May 18, 2018 at 5:11
  • That is not a button, but hidden field. Commented May 18, 2018 at 5:12
  • I might be wrong but I think <input name="submit" type="hidden" value="Reverse Geocode"> is what you are looking for. If it's not then why not just use onload event to trigger something? Commented May 18, 2018 at 5:15
  • What is your goal actually? You are asking about button which is "clicked" by default, but your html shows an hidden input field. Commented May 18, 2018 at 5:17

5 Answers 5

7

You can do as following:

<script type="text/javascript">
document.getElementById("submit").click();
</script>
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

2

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();

Comments

2

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();

});  

Comments

1

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();

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.