Sema Hernández
Sema Hernández

Reputation: 3

Trigger click after load page

I'm working with a wordpress theme and I need to activate a button when the page loads, but none of the following has worked for me (my button has the ID "reset"):

$(document).ready(function() {
   $("#reset")[0].click();
}); 

--

$(document).ready(function(){
   $("#reset").click();
})

--

$(window).load(function(){
      $('#reset').click();
});

I put the code in the header or in the page where i need to activate the button, but does not work.

Thanks!

Upvotes: 0

Views: 5434

Answers (6)

Willy David Jr
Willy David Jr

Reputation: 9131

This what works for me:

$(function () {
        $('#btnUpdatePosition').click();  
});

On the button event, the JQuery binding event doesn't work:

$('#btnUpdatePosition').click(function () {
            alert('test again');
        });

But it works when I added the event on attribute declaration:

<input type="button" value="Update" id="btnUpdatePosition" onclick="alert('Click has been called')" />

You can also call if you have a function on element:

<input type="button" value="Update" id="btnUpdatePosition" onclick="fncShow();" />

Upvotes: 0

flowDsign
flowDsign

Reputation: 76

You may need to refer to it using jQuery instead of $:

The jQuery library included with WordPress is set to the noConflict() mode...

https://codex.wordpress.org/Function_Reference/wp_enqueue_script#jQuery_noConflict_Wrappers

Upvotes: 1

Paris
Paris

Reputation: 6761

JavaScript does not allow seamless programmatic triggering of an actual click event.

What you could do is

  1. declare the click callback as a separate, named function

e.g.

function myClickCallback(e) {
  // Do stuff here
}
  1. Set this as the click callback of your button (e.g. $('#reset').on('click', myClickCallback)).

  2. Invoke the callback when the page loads (e.g. $(document).ready(myClickCallback);)

I am not sure why you 'd want this functionality, since it sounds weird. From reading your description, by "activating", you could also mean enabling the button. To do that you should do something like the following

$(document).on('ready', function (e){
    $('#reset').removeAttr('disabled');
});

Upvotes: 1

Med.Amine.Touil
Med.Amine.Touil

Reputation: 1235

Use the function Trigger with the event click

$(document).ready(function(){
 $("#reset").trigger('click');
});

Upvotes: 0

temonehm
temonehm

Reputation: 126

I give you example here

$(document).ready(function(){
   $("#reset").click();
})

the code above should work.

Upvotes: 1

Vali S
Vali S

Reputation: 1461

$(document).ready(function(){
 $("#reset").trigger('click');
});

Upvotes: 0

Related Questions