-1

I am trying to write a jQuery Ajax function, to reduce the amount of code in the page because the script is called a few times in the page. I can't seem to get it to work. Here is the code i have:

var loadURL = $(this).attr('href');
function load(){
$.ajax({
    url: loadURL,
    type: 'GET',
    cache: true,
    data: {
        delay: 4
    },
    success: function(data) {
        $('#load').html(data)
    }
    });
return false;}
$('#one').click(function(){ load(); });
$('#two').click(function(){ load(); });
$('#three').click(function(){ load(); });
<a href="one.php" id="one">Two</a>
<a href="two.php" id="two">Two</a>
<a href="three.php" id="three">Two</a>

can anyone guide me here please?

1
  • where do you have this line of code ??var loadURL = $(this).attr('href'); Commented Dec 18, 2010 at 6:21

1 Answer 1

2

i think the way you are getting href is wrong

function load(item){
var loadURL = $(item).attr('href');
$.ajax({
    url: loadURL,
    type: 'GET',
    cache: true,
    data: {
        delay: 4
    },
    success: function(data) {
        $('#load').html(data)
    }
    });
return false;}
$('#one').click(function()
{ 
load($(this)); 
return false; //you should mention return false here
});
Sign up to request clarification or add additional context in comments.

7 Comments

this almost works for me. the information is injected into the container, but the function does not return false, and the page is still loaded. Thanks for all of the help. i really appreciate this.
@mcbeav , return false should definetly kill the default behavior of href , i am 100% sure
@mcbeav , see updated answer you should have return false in the click event....handler
I actually tried that, and for whatever reason, the function no longer works. Nothing happens when i click the link.
what if i changed the function to use .ajaxSetup()
|

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.