0

I'm trying to do a click in a html button through javascript, but I can't make it work.

This is the button I'm trying to click with javascript (it works when I click it)

<button id="btnClean" runat="server" type="button" class="btn btn-warning btn-cons m-b-10" onserverclick="btnClean_Click">
 <i class="fa fa-trash-o"></i>
 <span class="bold m-l-3">Limpar</span>
 </button>

This is the button that calls the javascript function:

<button id="btnCreateOS" type="button" class="btn btn-success btn-cons m-b-10" onclick="Clean();">
 <i class="fa fa-save"></i>
 <span class="bold m-l-3">Criar</span>
</button>

And this is my javascript (it is located under <form> tag in my .aspx page):

<script type="text/javascript"> 
 function Clean() {
 document.getElementById("btnClean").click();
 }
</script>

And finally, this is the asp method:

public void btnClean_Click(object sender, EventArgs e)
{
 CleanFields();
}

Any ideas what I'm doing wrong?

asked Nov 30, 2016 at 11:33
17
  • Any error in the console. Commented Nov 30, 2016 at 11:35
  • When you debug this, where/how specifically does it fail? Are there any errors client-side or server-side? Is the client-side function invoked? Is the second button found by its id? Is the post-back invoked? Commented Nov 30, 2016 at 11:36
  • move your javascript to after closing body tag Commented Nov 30, 2016 at 11:37
  • document.getElementById("btnClean").click(); will call client event of button Commented Nov 30, 2016 at 11:37
  • @ZakariaAcharki got this error in console: Cannot read property 'click' of null at at HTMLButtonElement.onclick. Commented Nov 30, 2016 at 11:38

2 Answers 2

2

Bsically what happens, when you are using master and content page then it always changes its id according to it default behavior.

function Clean() {
 //document.getElementById("btnClean").click()
 document.getElementById('<%= btnClean.ClientID %>').click();
 }

Another solution you just add ClientIdMode="Static".

<button id="btnClean" runat="server" clientidmode="Static" type="button" class="btn btn-warning btn-cons m-b-10" onserverclick="btnClean_Click">
 <i class="fa fa-trash-o"></i>
 <span class="bold m-l-3">Limpar</span>
 </button>

in your button..

Then your current javascript code will also work..

function Clean() {
 document.getElementById("btnClean").click();
 //document.getElementById('<%= btnClean.ClientID %>').click();
 }
answered Nov 30, 2016 at 11:46
Sign up to request clarification or add additional context in comments.

1 Comment

Nice!!! Thank you so much for this! And thank you for your explanation too.
0

You should use

document.getElementById("btnClean").trigger("click");

instead, because it's faster. Maybe you should also try to prevent default behaviour in btnClear_Click? Try to add a console.log("a") (or rather "b" at the second) to your two functions, to see, if and which of the two functions are running.

answered Nov 30, 2016 at 11:42

2 Comments

Got the same error in console: Uncaught TypeError: Cannot read property 'trigger' of null(...)
1) .trigger is a jQuery method, not native javascript. 2) Even if this worked, it would be semantically the same as OP's existing call to click(). 3) even if this worked, it would only trigger the client-side event of btnClean, which the button doesn't have - it has a server-side event which the OP is trying to call.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.