Get 'this' element into a link [javascript]

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Get 'this' element into a link [javascript]

Post by Chalks »

I have a list item that is removed from the list when it is clicked on.

Code: Select all

<li onclick="remove(this)" style="cursor: pointer;">
instead of using 'onclick' though, I want 'this' to be in a link:

Code: Select all

<li><a href="javascript&#058;remove(this)">x</a></li>
However, the link (obviously) doesn't work. How can I make the href point to the correct element without knowing the list item index? I tried

Code: Select all

<a href="javascript&#058;remove(this.parentNode)">x</a>
and similar things, but I'm sure I'm using that incorrectly. Any ideas?


Thanks.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Get 'this' element into a link [javascript]

Post by pickle »

I don't think you can put Javascript like that in the href of a link. You could point the link to '#', and add an onclick() listener to the link. That'd behave just like a link normally would.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: Get 'this' element into a link [javascript]

Post by Chalks »

pickle wrote:I don't think you can put Javascript like that in the href of a link. You could point the link to '#', and add an onclick() listener to the link. That'd behave just like a link normally would.
ah, sweet. Why didn't I think of that before?

Thanks very much.
Post Reply