Page 1 of 1

Get 'this' element into a link [javascript]

Posted: Tue Oct 07, 2008 12:42 pm
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.

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

Posted: Tue Oct 07, 2008 12:50 pm
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.

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

Posted: Tue Oct 07, 2008 1:19 pm
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.