Harshana
Harshana

Reputation: 7647

JQuery function call with out an event

Suppose i have below jq function,

$(document).ready( 
  function(){ $('#demo_btn').click( 
         function(){ $.popup.show("The title", "A nice message"); 
  } ); 
}); 

I need to call the above function with out a click event. For example suppose a i check a condition and if its > than 10 im showing the above message. So its like no click event but just calling the above function when a condition satisfy after the page load.

More details its like im set a value for a variable in the controller and from view tpl im acccess it. So if the variable is >10 im showing the above alert. So if the variable is >10 the above alert should be display with out a click on a buton.

Thanks.

Upvotes: 0

Views: 199

Answers (4)

Karoly Horvath
Karoly Horvath

Reputation: 96258

use trigger.

<?php if ($var > 10) { ?>
    $('#demo_btn').trigger('click');
<?php } ?>

Upvotes: 1

James Allardice
James Allardice

Reputation: 165941

If I've understood your question correctly, you can do something like this:

$('#demo_btn').click(doStuff);

function doStuff() {
   $.popup.show("The title", "A nice message");
}

You can then call the doStuff function as normal anywhere else in your code.

Update... Having reread your question, I think you might actually be looking for something that is constantly checking to see if a variable has a certain value. If that's the case, you can use setInterval:

var x = 1;

setInterval(doStuff, 500);

function doStuff() {
    if(x > 10) {
        //Do stuff
    }
}

The above code checks the value of x to see if it's greater than 10 every 500 milliseconds. Here's an example fiddle showing the above concept in action.

Upvotes: 0

devtut
devtut

Reputation: 697

when you are using a php, you can use it like below

<script>
$(document).ready(function(){ 
  <?php if($variable>=10){?>
 $.popup.show("The title", "A nice message"); 
 <?php }?>
}); 
</script>

Upvotes: 0

Kishore
Kishore

Reputation: 1912

You can create a function in javascript if it satisfies the above condition then call the popup function.

if you can post the whole code then it will be easy to suggest an exact solution.

Upvotes: 0

Related Questions