populate array fron list onclick javascript

Posted by user3703591 on Stack Overflow See other posts from Stack Overflow or by user3703591
Published on 2014-06-03T14:59:46Z Indexed on 2014/06/03 15:25 UTC
Read the original article Hit count: 151

Filed under:
|
|
|
|

I 'm writing a code with JS and I don't know how to populate array when clicking on button. We have this code, which uses a list (ul), where the items (li) can be moved with mouse. How can do onclick to populate an array with 2 data, its first position and the last position?

    <!doctype html>
    <html lang="en">
    <head>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>    
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>   
    <script src="https://raw.github.com/furf/jquery-ui-touch-punch/master/jquery.ui.touch-punch.min.js"></script> 
    <script>
      $(function() {
        $( ".documents" ).sortable();
        $( ".documents" ).disableSelection();
      });
    </script>
  <meta charset="utf-8">
  <title>toArray demo</title>
  <style>
  span {
    color: red;
  }
  </style>
</head>
<body>

Reversed - <span></span>

<ul id="opciones" class="documents">
<li>uno</li>
<li>dos</li>
<li>tres</li>
</ul>

<script>
function disp( li ) {
  var a = [];
  for ( var i = 0; i < li.length; i++ ) {
    a.push( li[ i ].innerHTML );
  }
  $( "span" ).text( a.join( " " ) );
}
disp( $( "li" ).toArray() );
</script>
 <input type="button" value="actualizar_array" onclick="disp('#opciones')" />
</body>
</html>

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery