Trigger a button on page load
Asked Answered
B

4

6

I have this function

$("a#<?php echo $custom_jq_settings['toggle']; ?>").click(function() {
        jQuery("#<?php echo $custom_jq_settings['div']; ?>").slideToggle(400);
        jQuery("#featurepagination").toggle();
        jQuery("#featuretitlewrapper").toggle();
        return false;
    });

And this is the button I want to trigger on page load

<a href="#" id="featuretoggle" onclick="changeText('<?php if (is_front_page()) {?>Show<?php } else { ?>Show<?php } ?> Features');"><?php if (is_front_page()) {?>Hide<?php } else { ?>Hide<?php } ?> Features</a>

I would like to trigger that button when the page loads so that it starts open but then slides/closes

Bowsprit answered 12/1, 2012 at 18:9 Comment(3)
What is the question? What problem do you encounter?Mandiemandingo
Sorry the problem is that when the page loads up the button works to slide the div up and down but I want the div to load open like it does but then slide up example firstlutheranomaha.org/who-we-areBowsprit
Also its the hide features, show features area im talking about, thanksBowsprit
L
11

Does this not work?

<script>
    jQuery(function(){
      jQuery('#featuretoggle').click();
    });
</script>
Lunalunacy answered 12/1, 2012 at 18:15 Comment(2)
I added this into the header and it doesn't work firstlutheranomaha.org/who-we-areBowsprit
You need to use jQuery instead of $. You should also put these scripts in the footer not the header. developer.yahoo.com/performance/rules.html#js_bottomLunalunacy
P
6

That's the easiest way:

<script>
    $(function() {
        $("#featuretoggle").trigger("click");
    });
</script>
Placative answered 12/1, 2012 at 18:14 Comment(0)
R
1

You can trigger a click event manually with jQuery:

$('#featuretoggle').click();

To do this when the page loads:

$(document).ready(function() {
    $('#featuretoggle').click();
});

I imagine you'll want this to be the last thing to happen when loading the page, so make sure it's the last line to be executed within $(document).ready().

See this example:

<a href="#" id="someButton">Foo</a>
<script type="text/javascript">
    $(document).ready(function() {
        // bind the click event
        $('#someButton').click(function() {
            alert('baz');
        });

        // trigger the click event
        $('#someButton').click();
    });
</script>
Recur answered 12/1, 2012 at 18:17 Comment(0)
P
0

What you want is can be achived by using setTimeout() function.

$(document).ready(function() {
    setTimeout(function() {
        $("a#<?php echo $custom_jq_settings['toggle']; ?>").trigger('click');
    },10);
});

This will work for you surely...

Pepi answered 22/1, 2013 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.