If there is no need for the additional function you could always just
write it like so:
$(document).ready(function() {
$('a.taglink').click(function() {
var t = $(#tags);
if(!(t.val()) {
t.val(this.txt);
return;
}
var ss = t.val().split(this.text);
if(ss.length <= 1) {
var wa = (t.attr('value')+',' +this.text).split(",");
wa.sort();
t.val(wa.join());
}
});
But then again if you're using the tagSelect function for another
purpose then stick with Christof's code :-)
Allan
Christof Donat wrote:
>
> Hi,
>
> > Just about getting feet wet with jQuery, wrote a simple function which
>
> > fills an input field with text of the link clicked after sorting the
>
> > comma separated values. It is working as expected but would like to
>
> > know if it can be made more comprehensive.
>
> Your code is really good for a jQuery beginner ;-) Just small
> improvements:
>
> $(document).ready(
>
> function(){
>
> // we can directly use the tagSelect-function here, because I changed
>
> // it to use this.text - what is this.text anway? Do you mean
>
> // $(this).html()?
>
> $('a.taglink').click(tagSelect);
>
> }
>
> );
>
> function tagSelect() {
>
> var t = $('#tags'); // this is used very often; if you meant
>
> // $(this).html(), store it in a local variable as
>
> // well, like
>
> // var txt = $(this).html();
>
> if( !t.val() ) {
>
> t.val(this.text);
>
> return;
>
> }
>
> var ss = t.val().split(this.text);
>
> if ( ss.length <= 1 ) {
>
> // don't use t.val to store a temporary value.
>
> var wa = (t.attr('value')+',' +this.text).split(",");
>
> wa.sort();
>
> t.val(wa.join()); // you always change t.val() again here
>
> }
>
> }
>
> Christof
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> jQuery mailing list
> [email protected]
> http://jquery.com/discuss/
>
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/