0

I have a simple aspx page. After loading the page I want a button to be pressed.

Here is the button that I want to click:

<asp:Button class="js-modal js-prevent-bg-click inline" id="invisibleSelectProducer" href="#" runat="server" Text="Select Producer"></asp:Button>

Here is the jquery that I have added that (I would have thought) should click the button once the page is finished loading:

<script language="javascript" type="text/javascript">
    $(document).ready(function () {
       console.log($("#invisibleSelectProducer"));
        $("#invisibleSelectProducer").click();
    });
</script>   

Am I missing something completely obvious? Even the log that I added (above) logs the button object that I want to click. But the code immediately following does nothing.

EDIT After trying Mike123's suggestion, I have the following code. It still does not work though. Once again, the console will let me see the button object (so I know it is rendered) but the .click() is not firing off once the page loads.

<script language="javascript" type="text/javascript">
    $(document).ready(function () {
       console.log($("#<%=invisibleSelectProducer.ClientID%>"));
        $("#<%=invisibleSelectProducer.ClientID%>").click();
    });
</script>   
1
  • Thanks Adrift. Was in the middle of adding better code sections, too. Commented Mar 11, 2015 at 20:06

1 Answer 1

0

Your server side control id of invisibleSelectProducer will be different on the client, you should either reference as either one by client id or class name

$("#<%=invisibleSelectProducer.ClientID%>").click();

or

$(".js-modal").click();

.js-modal is the class you have in your markup, but you might want to use custom one

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, i was on the dude version of maternity leave last week :) I have changed my code to reflect what you suggested and it still does not click on page load.
Holt, have you made a progress? The click event may not work if the button is hidden. You think it could be the case?
I was using a custom modal class that someone else creates. Turns out...they had logic that prevented the modal from being clicked until after the page's init was completely done. Rather dumb...but I figured a way around it.
Even though this suggestion didn't help me in my particular issue...it obviously is the correct way to click a button.

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.