2

I was wondering if someone would be able to help or point me in a direction. I am looking to make a web application like a quiz. What I need is to be able to load dynamic content off a dynamical created button or href in this case that will link to the other content. So far it doesnt seem to be functioning in that i get a page not found every time i click the button. doesnt seem to be passing the .php through the name. I have tested this outside of the dynmaic content div and it works fine but i really need it to work inside. What I have so far:

index.php

<body>
<div class="container_12">

    <div id="content">
    <!--dynamic content loads here-->
    </div>
</div>  

</body> 

Login.php

<h1> Login Page </h1>
    <p> this is a test to c if works </p> 
        <a href='Suitable'>Hello</a> <!--button that doesnt work-->

general.js

$(document).ready(function() {

//initial
    $('#content').load('Login.php');

    //Handle widget Clicks 
    $('div#content a').click(function() {
        var page = $(this).attr('href');
        $('#content').load('pages/' + page +'.php');
        return false;

    });
});

Suitable.php

<h1> Working </h1>
<!-- content thats not loading off button-->

3 Answers 3

2

you should delegate the click event for dynamically loaded elements, try the following:

$('#content').on('click', 'a', function(e) {
    e.preventDefault()
    var page = $(this).attr('href');
    $('#content').load('pages/' + page +'.php');
});
Sign up to request clarification or add additional context in comments.

Comments

0
$(document).ready(function() {

    //initial
    $('#content').load('Login.php', function(){
        //Handle widget Clicks 
        $('div#content a').click(function() {
            var page = $(this).attr('href');
            $('#content').load('pages/' + page +'.php');
            return false;

        });

    });

});

Explanation:

load is asynchronous. JS engine does not wait for it to complete before it goes to the next line. The result is that when your click binding executes there is no a in div#content, so the selector returns an empty array and no binding happens.

Comments

-1

Try Live instead of click

//Handle widget Clicks

$('div#content a').live('click',function() {
    var page = $(this).attr('href');
    $('#content').load('pages/' + page +'.php');
    return false;

});

Comments

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.