views:

40

answers:

2

hello, i have the below sample code for a navigation list:

<div id="menu">
 <ul>
  <li><a href="#sect1">link 1</a></li>
  <li><a href="#sect2">link 2</a></li>
  <li><a href="#sect3">link 3</a></li>
 </ul>
</div>

and some jquery code:
$("#menu li").click(function () {
  var mylicontent=$(this).html();
});

i want to get both html content of the li object and the href value. Any ideas?

+4  A: 

You can do like:

$("#menu li").click(function () {
  var mylicontent = $(this).html();
  var mylik = $(this).find('a').attr('href');
});

Or you can also do like:

$("#menu li").click(function () {
  var mylicontent = $(this).html();
  var mylik = $('a', this).attr('href');
});
Sarfraz
that was so easy! thanks!
Sotos
@Sotos: You are welcome :)
Sarfraz
I would use delegate here rather than x click events
redsquare
delegate..., what exactly do you mean?
Sotos
http://api.jquery.com/delegate/
redsquare
Nice! Sarfraz, a little suggestion in your first option we can take $(this) in a separate variable and can use that variable multiple times instead of doing $(this) again in that method.
Raghav Khunger
+2  A: 

It's best if you attach click on the a and not on the li.

Then you'd do something like $(this).parent().html()

cherouvim
and the href would be $(this).attr('href');right?
Sotos
@Sotos: exactly
cherouvim