jquery - Javascript - Swap input element on click -


saw on xe.com swap currency on form. need site also. not question, solve problem , sharing here:

html

<form target=""> <input name="input1" id="firstinput" value="input #1"> <input name="input2" id="secondinput" value="input 2"> </form> <a class="swap">swap</a> 

javascript

$(document).ready(function () {     $("a.swap").click(function(){         var axix = document.getelementbyid("firstinput").value;         var byiy = document.getelementbyid("secondinput").value;         document.getelementbyid('secondinput').value = axix;         document.getelementbyid('firstinput').value = byiy;      }); }); 

demo: http://jsfiddle.net/z5vse/

any better way it? share us. :)

shorter (not "better"):

$("a.swap").click(function () {     $('#pickup').val([$('#destination').val(), $('#destination').val($('#pickup').val())][0]) }); 

jsfiddle example

imo "better" mean clean, clear, , concise.

$("a.swap").click(function () {     var temp = $('#pickup').val();     $('#pickup').val($('#destination').val())     $('#destination').val(temp); }); 

Comments

Popular posts from this blog

php - render data via PDO::FETCH_FUNC vs loop -

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

The canvas has been tainted by cross-origin data in chrome only -