3

I have made a list by accessing elements from an array randomly(Using JavaScript, used this link Getting a random value from a JavaScript array) and then print them all (in first div). Now in another div I want to access that all elements but I am getting results like LI,BR etc. whatever i mentioned when i appended elements to div.(I used this link Get all elements nested in UL tag )

Here the code is:

<div   id="list1" >
                        <ol id="olMediators">

                         </ol>
                    </div>

For random accessing I am using function

function getRandomNumber()
    {

    var items= ["Mediator 1","Mediator 2","Mediator 3", "Mediator 4","Mediator 5","Mediator 6","Mediator 7","Mediator 8","Mediator 9"];

    var newitems=[];
        for(var i=0;i<5;i++){
    var item = items[Math.floor(Math.random()*items.length)];


        $("#olMediators").append('<li>'+item+'</li>'+'<br/>' );


     }
    }

And for print them to another div I am using jquery (Onclick on button):

$('#getMediators').click(function(){

                $('#radioMediators').hide();

               var el = document.getElementById("olMediators").getElementsByTagName("*");
                for (var i=0; i<el.length; i++) {
                 alert(el[i].tagName);
                }

So what I did wrong?? How can I get these all elements and print them to some other place(In different div)??

Thanks

Community
  • 1
  • 1
Shyam Dixit
  • 2,325
  • 3
  • 19
  • 27
  • `br` isn't a valid element within `ol` elements, only `li` is. You're also mixing JavaScript and jQuery unnecessarily, you'd benefit from http://try.jquery.com. – James Donnelly Oct 08 '13 at 12:26
  • I checked with jquery and javascript code also but it is not working. Any guess? – Shyam Dixit Oct 08 '13 at 13:14

2 Answers2

1

try

$('#getMediators').click(function () {

    $('#radioMediators').hide();

    var el = $('#olMediators *').get();
    for (var i = 0; i < el.length; i++) {
        alert(el[i].tagName);
    }
});

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
1

pure jquery solution:

$('#getMediators').click(function(){
    $('#radioMediators').hide();
    $("#olMediators li").each(function(){
       alert($(this).text());
    });
});

Here is the Fiddle

codingrose
  • 15,563
  • 11
  • 39
  • 58