Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
43
rated 0 times [  48] [ 5]  / answers: 1 / hits: 25238  / 12 Years ago, wed, march 7, 2012, 12:00:00

This may be something very easy but i can't seem to get this to work and im not sure why. I have jquery installed and i am trying to get an attribute of this element when i click on it. Right now my code looks like this:



url = $(this).attr(href)


When I call this function by clicking on a link, it tells me that the var url is undefined. So obviously it is not picking up the this when i click on the link. I am trying to pass the href of an anchor tag to use as my variable.



What am i overlooking? Again, i know this is something very simple but i can't seem to figure it out so thank you for taking the time to help me.



Thanks.



<script type=text/javascript>
url = push1;

$(a).live(click, function(event) {
event.preventDefault();
url = $(this).attr(href);
})
$.ajax({
type: get,
url: /+url+.php,
data: ,
dataType: html,
success: function(html){
jQuery('#Right_Content').hide().html(html).fadeIn(1000);
},

})
;
</script>


html:



<body>

<a href=push1 >Image 1</a>
<a href=push2 >Image 2</a>

<div id=Right_Content></div>

</body>

More From » jquery

 Answers
18

This should work for you



$(function(){    
$(.link).click(function(){
var url=$(this).attr(href);
alert(url);
return false;

});

});​


Assuming you are targeting all anchor tags with a css class called link



Here is the working example : http://jsfiddle.net/L99mM/2/



Edit: As per your code posted in the question



You should call preventDefault after your ajax call. and there is closing brackets should be after ajax call



$(a).live(click, function(event) {      
var targeturl = $(this).attr(href);

$.ajax({
type: get,
url: /+targeturl +.php,
data: ,
dataType: html,
success: function(html){
jQuery('#Right_Content').hide().html(html).fadeIn(1000);
}

}); // closing for ajax
event.preventDefault();

}); // closing for click event binding

[#86998] Tuesday, March 6, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
morrismilom

Total Points: 230
Total Questions: 96
Total Answers: 114

Location: Mayotte
Member since Mon, Sep 12, 2022
2 Years ago
;