Run jquery without click function

I am using disclosure in one link, with this code and it works:

$(document).ready(function() {
  var triggers = $("a[rel]").overlay({
    expose: {
      color: '#212121',
      loadSpeed: 200,
      opacity: 0.9
    },
    closeOnClick: false
  });
});

      

Link:

<div id="triggers"><a href="" rel="#pop_member">Click here</a></div>

      

But I want to run the code printed in php to change it to:

$(document).ready(function() {
  function run_expire(){
    var triggers = $("#pop_member").overlay({
      expose: {
        color: '#212121',
        loadSpeed: 200,
        opacity: 0.9
      },
      closeOnClick: false
    });
  }
});

      

and print in php:

echo '<script type="text/JavaScript">run_expire();</script>';

      

but it doesn't work.

+2


source to share


3 answers


The problem is that the function run_expire

is only defined in the function document.ready()

, so the code in the script tag cannot find it.

There is no need to use here $(document).ready()

. The best solution would be to simply this:

function run_expire(){
  var triggers = $("#pop_member").overlay({
    expose: {
        color: '#212121',
        loadSpeed: 200,
        opacity: 0.9
    },
    closeOnClick: false
  });
}

      

You might want to make sure the document is ready before launching run_expire

, although you can do it like this:

echo '<script type="text/JavaScript">$(run_expire);</script>';

      

Edit

Looking back at your code, there is another problem. $("#pop_member")

will not select a link defined like this:

<a href="" rel="#pop_member">

      

If you want the selector to work, you will need to do this:



<a href="" id="pop_member">

      

However, this may not be what you want, because there can only be one element on the page with the same attribute id

. The attribute rel

is not appropriate here. You probably want to define it with a class, for example:

<a href="" class="pop_member">

      

with this selector:

$(".pop_member")

      

Alternatively, if you really want to select the link the way you wrote it, you can use this selector:

$("a[rel='#pop_member']")

      

Edit 3

Your real problem was formulated in this question , namely that the jQuery tool library was not being used properly. You need to pass api: true

to the overlay function and call .load()

on the return value of the overlay function for the programmatic overlay to appear on the load page.

+4


source


try this:



echo '<script type="text/JavaScript">$(function () { run_expire(); });</script>';

      

+1


source


This is because your function is run_expire

defined inside the scope of the $ (document) .ready event handler.

You can define your function outside:

function run_expire(){
    var triggers = $("#pop_member").overlay({
        expose: {
            color: '#212121',
          loadSpeed: 200,
          opacity: 0.9
        },
        closeOnClick: false
    });
}

$(document).ready(function() {
    run_expire();
});

      

This will define run_expire

in the global scope and your generated PHP script will work.

+1


source







All Articles