I have an onclick function for an <ul><li>,
but I need to set a variable from the id of an <a href> tag insde the <li> and I'm not sure how.

I have:

$('#dropdown ul li').click(function(){

var picked = $(this).attr('id'); // this part I don't know.
// var picked = $(this).href('id'); didn't work.

$('picked1').innerHTML = picked ;


 /* pulling from a list of items that look like:

 <li id='name_1'><a id="name_a">text1</a></li>
 <li id='name_2'><a id="name_b">text2</a></li>
 <li id='name_3'><a id="name_c">text3</a></li>

 If I clicked 'name_2', (this), i need to set a variable from 'name_b', within it.

Recommended Answers

All 7 Replies

I'm not entirely sure I understand what you are trying for. What is the variable you need and where do you want to put it?

And can I ask, what are you trying to do? Having one of the navigation items changed when you are on a page?

hai turpentyne,

i am getting confusion about your requirement

can you clearly explain me about the requirement?

If I clicked 'name_2', (this), i need to set a variable from 'name_b', within it

so that i will try to give a way to solution

happy coding

Actually, I found a way around it, with a simpler method to do what I need. But, I'm still curious to konw. So, if I were to boil it down - how do I get the id of elements inside whatever $(this) is.

So, if it's an <li> with php generated id's, and I don't know what those id's are. How do I get the id of an element inside of that clicked <li>. In this case it was an <a> tag.

really??? haha! It didn't work for me, so I just naturally assumed I wasn't doing the right thing.

The only difference with my example is that the click event is set to the "a", where you have it set to just the li.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.