0

I'm trying to get values from my HTMLOListElement object, but allways return undefined.

This is my function:

$("#finish").click(function(){
    var elems = document.getElementById( "selectable2" );
    var arr = jQuery.makeArray( elems );
    arr.reverse();              
    confirm("It will generate the group: " +groupname+ " with that users:" +arr+"."); 
});

selectable2 - this is a list on Jquery.

Finally allways get the same from the Confirm windows:

It will generate the group: examplegroup with that users:[object HTMLOListElement].

How can i get the values tag from that object?

Thanks everyone. Marc.

1 Answer 1

1

If you want a list of li's you have to specify that in selector passed to $.makearray() so you could use querySelectorAll as document.querySelectorAll("#selectable2 li"), check the example below.

Hope this helps.

$("#finish").click(function(){
  var groupname = "examplegroup";
  var elems = document.querySelectorAll("#selectable2 li");
  var arr = jQuery.makeArray( elems );
  arr.reverse();        
  
  console.log(arr);

  console.log("It will generate the group: " +groupname+ " with that users:" +arr+".");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ol id="selectable2">
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ol>
<button id="finish">Finish</button>

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Zakaria!!! It works, but finally I simplified to this: var users =[]; $("#selectable2 li").each(function() { users.push($(this).text()); });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.