Friday, January 13, 2012

latch onto children of an element with jQuery

I rewrote what is here a smidge in the name of getting it under a Jasmine test. In doing so, my rewrite put relevant HTML controls in a wrapping div and then interacted with them as children of the div.

 
 

myscript.js:

$(document).ready(function() {

   $("#wrapper").children(":button").click(function() {

      var leftvalue = $("#wrapper").children(0).val();

      $("#wrapper").children("#righty").val(leftvalue);

   });

});

 
 

myscript.js is called by:

<!DOCTYPE html>

<html lang="en">

   <head>

      <script src="jquery-1.5.js" type="text/javascript"></script>

      <script src="myscript.js" type="text/javascript"></script>

   </head>

   <body>

      <div id="wrapper">

         <input id="lefty" />

         <button type="button" id="mover">Move Right -></button>

         <input id="righty" />

      </div>

   </body>

</html>

 
 

Content put in the left text field gets moved to the right text field when the button is clicked.

No comments:

Post a Comment