Patrioticcow
Patrioticcow

Reputation: 27038

php, jquery, how to trigger a click?

i have a link that needs to be triggered from a success post:

<?php    
if ($_POST["action"] == "1") {
?>
<script type='text/javascript'> 
$(window).load(function() {
    $(".likepic").click();
});
</script>
<?php } ?>

<script type='text/javascript'> 
$(window).load(function() {
$(".likepic").click(function(){
    $(".likepic").colorbox({width:"620px", height:"570px", inline:true, href:"#likepic_lightbox"});
});
});
</script>
<a href="#" class="likepic"></a>
<div class="blackk" style="display:none;">
<div id="likepic_lightbox">test
</div>
</div>

so if that post action is 1 then run the jquery script and click on the link for something else to happen :)

this is what i tried but without success.

any ideas? Thanks

Upvotes: 0

Views: 3098

Answers (1)

irama
irama

Reputation: 658

Try using $(document).ready() instead of $(window).load()

Also, you'll need to switch the order of your JavaScript blocks. The click handler needs to be defined first.

Play with a test version here: http://jsfiddle.net/irama/bcMp7/

Or see updated code below:

<script type='text/javascript'> 
    $(document).ready(function() {
        $(".likepic").click(function(){
            $(".likepic").colorbox({width:"620px", height:"570px", inline:true, href:"#likepic_lightbox"});
        });
    });
</script>

<?php if ($_POST["action"] == "1") { ?>
<script type='text/javascript'> 
    $(document).ready(function() {
        $(".likepic").click();
    });
</script>
<?php } ?>


<a href="#" class="likepic"></a>
<div class="blackk" style="display:none;">
    <div id="likepic_lightbox">test</div>
</div>

Let us know how you go!

Upvotes: 2

Related Questions