0

I have a question, I am stumped as to why my code isnt working, I am trying to load a PHP file into a div upon click of a list item anchor tag.

Code:

<script type="text/javascript">
$(document).ready(function() {


//I have these 2 functions aswell that DO work

 $(".itm1 a").click(function(evt) {
     var page = $(this).attr('href');
     $("#page1").load(page);
     evt.preventDefault();
  });

  $(".footnav a").click(function(evt) {
     var page3 = $(this).attr('href');
     $("#page1").load(page3);
     evt.preventDefault();
  });
// ---------------------------

  $(".needslist a").click(function(evt) {
     var page4 = $(this).attr('rel');
     $("#page1").load(page4)
     evt.preventDefault();
  });



});
</script>


<ul>
 <li class="needslist"><span><a rel="/ceos.php" href="#">Are you a CEO SPEECH-MAKER or ENTREPRENEURIAL VISIONARY?</a></span><br />
            who wants the best advice and counsel from an expert coach for your own speeches and leadership events that include speaking to investors, stakeholders, business collaborators, board of directors, conferences and/or the media?</li><br />

 </ul>

Can anyone offer an explination to why its not working?

EDIT: Okay Guys, So I found out once I load a div by ajax, that content becomes unable to process jquery. This is my problem.... How do I execute jquery scripts on loaded content????!!!

4
  • 1
    what is #page1 here? Also a typo missing ; after load Commented Aug 15, 2012 at 19:00
  • #page1 is the div im trying to load the php file into... and no errors Commented Aug 15, 2012 at 19:05
  • you are testing locally? Commented Aug 15, 2012 at 19:10
  • EDIT: Okay Guys, So I found out once I load a div by ajax, that content becomes unable to process jquery. This is my problem.... How do I execute jquery scripts on loaded content????!!! Commented Aug 15, 2012 at 19:52

1 Answer 1

1

Found a solution:

Once you load ajax content, you cant execute any scripts on that loaded content, heres my solution:

$(".needslist a").live("click", function(evt) {
     var page4 = $(this).attr('rel');
     $("#page1").load(page4);
     evt.preventDefault();
  });
Sign up to request clarification or add additional context in comments.

1 Comment

Using live() is not recommended it has many drawbacks and issues. Use on() which is the recommended method since jQuery 1.7 or use delegate() pre 1.7 for binding to dynamic elements and bind() for static elements. See jQuery documentation on live() for a list of the issues: api.jquery.com/live

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.