Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access all elements of an orderedlist

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

like image 263
Shyam Dixit Avatar asked Jan 26 '26 03:01

Shyam Dixit


2 Answers

pure jquery solution:

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

Here is the Fiddle

like image 128
codingrose Avatar answered Jan 28 '26 17:01

codingrose


try

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

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

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

Demo: Fiddle

like image 39
Arun P Johny Avatar answered Jan 28 '26 17:01

Arun P Johny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!