Wednesday, 15 September 2010

jquery - How to shortened my JQ Script -


I write some jquery scripts to add a new display name option on the front-end like wp back-end. Choose with id #display_name #nickname, #last_name, or #nickname change value when automatically add new options.

And it works fine!

But my code is too long.

How to narrow down my JQ script?

  var alias = jQuery ('#name'); Var first_name = jQuery ('# first_name'); Var last_name = jQuery ('# last_name'); Nickname.change (function () {jQuery ('# display_name'). Append ($ ('& lt; option & gt;', {value: nickname.val (), text: nickname.val ()})); }); ('$' And 'option & gt;', {value: first_name.val (), text: first_name.val ()})); JQuery ('(' # display_name ')) Addendum ($ (' & lt; option & gt; ', {value: last_name.val ():' '+ first_name.val (), text: last_name.val () + '' + First_name.val ()});) JQuery ('# display_name') .andend ($ ('& lt; option & gt;', {value: first_name.val ()) + '' + last_name.val (), Text: first_name.val () + '' + Last_name.val ()}))}}; Last_name.change (function () (jQuery ('# display_name'). Append ($ ('(>> option & gt;', {value: last_name.val (), text: last_name.val ()}) ); JQuery ('Adjustment & gt; (' (lt; option & gt; ', {value: last_name.val ():' '+ first_name.val (), text: last_name.val () +' '+ first_name Val ()});) JQuery ('# display_name') .andend ($ ('& lt; option & gt;', {value: first_name.val ()), '' + last_name.val (), text: First_name.val () + '' + Last_name.val ()}));});    

This is your small code

  $ ("# Nickname, # first_name, # last_name"). Change (function () {$ ('# display_name'). Append ($ ('& Lt; Option & gt;', {Value: $ (this) .val (), text: $ (This) .val ()}; if ($ (this) .attr ("id")! = "Nickname") {var f Irst_name = jQuery ('# first_name'); var last_name = jQuery ('$ (' # display_name '); $ (' $ lp; option & gt; ', {value: last_name.val () +' '+ first_name. Val (), text: last_name.val () + '' + first_name.val ()})) .andend ($ ('& lt; Option & gt; ', {value: first_name.val () +' '+ last_name.val (), text: first_name.val () +' '+ Last_name.val ()})))}}});   

Demo

No comments:

Post a Comment