Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Hopefully someone can give me some help.

I use the code below to add tags to an input field. However when adding a tag the entire 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 clicking a span it's supposed to return the text 'link 1', however it now returns 'http://www.example.com/create/link 1'

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

Cheers, G.

like image 863
Goose Avatar asked Feb 13 '11 00:02

Goose


1 Answers

I guess the spans are nested within an anchor ?

If so, don't use event.target (which not necessarilly represents your .ltags-add class), but j(this).text() which always references your <span> to which the click event was bound.

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

like image 179
jAndy Avatar answered Nov 09 '22 20:11

jAndy