cfocket
cfocket

Reputation: 109

onClick event open page then load function

On #page2, I have an hidden content triggered by the function openContent().

On #page1, I'd like a href tag to load #page2 then load the openContent() function.

I tried to create a onClick event that would do both on #page1 but it doesn't work.

I'm new to jQuery and I must have it all mixed up.

Thanks

Upvotes: 0

Views: 855

Answers (3)

MisterJames
MisterJames

Reputation: 3326

If you need to do this with transient content, or by a trigger from the second page, you can use jQuery's .on() to attach a handler for these types of scenarios.

http://api.jquery.com/on/

A simple example (from the wiki):

$("#dataTable tbody tr").on("click", function(event){
  alert($(this).text());
});

You would use this to wire up a click event, for example, to any tr that exists on the page at any time (doesn't need to exist).

You could use this strategy to wire up your on click for page 2 on page 1, then it will only take effect when it's loaded via page 1.

Cheers.

Upvotes: 0

Rahul
Rahul

Reputation: 438

Why don't you call your function in #page2 onload

<body onload="openContent()">

Upvotes: 2

Samsquanch
Samsquanch

Reputation: 9146

Page 1: <a href="page2.html#open">Page 2</a>

Page 2

jQuery(document).ready(function($) {
  if(window.location.hash == "open") {
    openContent();
  }
});

Upvotes: 5

Related Questions