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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With