Click here to Skip to main content
15,905,877 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello,

I want to read the values in tags <ul><li></li></ul> with JavaScript, and then save the values in an array. I don't want to use jQuery.

for example:
JavaScript
<ul>
 <li>1</li>
 <li>2</li>
 <li>3</li>
</ul>

now my array is :
arr[0]==1;
arr[1]==2;
arr[2]==3;


How can I write this script?
Posted
Updated 24-Jan-14 15:08pm
v2

Use something like this:

C#
var list = document.getElementById('ulid');
var list_items = list.getElementsByTagName("li");
 
Share this answer
 
Comments
NorouziFar 25-Jan-14 0:14am    
give me this error in F12(browser): TypeError: list is null

please give me full example
If your HTML only only one UL Tag as your sample code, this will work for you. else you need to modify it according to your need

JavaScript
var ul = document.getElementsByTagName('ul');
            var li = ul[0].getElementsByTagName('li');
            var array = new Array();
            for (var i = 0; i < li.length; i++) {
                array.push(li[i].innerText)
            }
            alert(array);
 
Share this answer
 
Comments
NorouziFar 25-Jan-14 0:15am    
give me this error in F12(browser): TypeError: ul[0] is undefined


please give me full example
Karthik_Mahalingam 25-Jan-14 0:57am    
Will give you after sometime,
now i am away from pc
- mobile
Karthik_Mahalingam 25-Jan-14 4:42am    
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

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

$(function () {

var ul = document.getElementsByTagName('ul');
var li = ul[0].getElementsByTagName('li');
var array = new Array();
for (var i = 0; i < li.length; i++) {
array.push(li[i].innerText)
}
alert(array);
debugger;

});



</script>
</head>
<body>
<form id="form1" runat="server">

<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>

</form>


</body>
</html>
NorouziFar 25-Jan-14 11:13am    
tankyou but you use jquery==$
Karthik_Mahalingam 25-Jan-14 11:29am    
no NorouziFar
it is not jquery
its a javascript
even if you remove this line " <script src="jquery.js.js"></script> " it will work :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900