(event.target) .text () returns url instead of text

Hope someone can help me.

I am using the following code to add tags to an input field. However, when you add a tag, the whole path is included.

j('.ltags-add').click(function (event){ contents = j('#link-tags').val(); if ( contents != '' ) { sep = ', '; } else { sep = ''; } tag = j(event.target).text(); j('#link-tags').val( contents + sep + tag ); }); 

<span class="ltags-add">link 1</span> <span class="ltags-add">link 2</span>

When you press the space bar, it should return the text "link 1", but now it returns "http://www.example.com/create/link 1"

Does anyone have an idea why this is happening and what can I do about it?

Cheers, G.

+6
jquery events text
source share
1 answer

I think spans nested in anchor ?

If so, do not use event.target (which does not necessarily represent your .ltags-add class), but j(this).text() , which always refers to your <span> that the click event was associated with.

See Demo : http://www.jsfiddle.net/YNUA5/1/

+5
source share

All Articles