• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

how to create a ul and li and then test the variable?

how to create a ul and li and then test the variable? Not working...

var variable = 0;

$("<ul id='set'><li> + variable + </li></ul>");

alert("check li variable value: " + $("#set li").val()); or alert("check li variable value: " + $("#set li").text());

Not working, returning undefined when using val()
and empty when using .text()
0
areyouready344
Asked:
areyouready344
  • 6
  • 4
2 Solutions
 
leakim971PluritechnicianCommented:
use :
$("<ul id='set'><li> + variable + </li></ul>");
instead :
$("<ul id='set'><li>" + variable + "</li></ul>")

and put it (append it) in the dom
Test page : http://jsfiddle.net/r7qJw/

var variable = 20111009;

var $ul = $("<ul id='set'><li>" + variable + "</li></ul>");

$ul.appendTo("#here"); // YOU MUST PUT IT IN THE DOM!!!

alert("check li variable value: " + $("#set li").html());
alert("check li variable value: " + $("#set li").text());

Open in new window

0
 
areyouready344Author Commented:
thanks leakim, it works but one quick question, how do I get the prev() in this list, I tried the following but not working.


alert("check li variable value: " + $("#set li").prev().html());
0
 
leakim971PluritechnicianCommented:
$("<ul id='set'><li>" + variable + "</li></ul>");
there's only one li
so there's no li before
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
areyouready344Author Commented:
Thanks for letting me know this but I have the ul list in an each loop. Is this syntax correct,

alert("check li variable value: " + $("#set li").val());
0
 
leakim971PluritechnicianCommented:
no, that's why I replace your previous code with html()
val() is for value
0
 
areyouready344Author Commented:
This is not working.

alert("check li variable value: " + $("#set li").prev().html());

But each time through the each loop, this value increments..

alert("check the total loop count of set list: " + $("#set li").length);


This means to me #set list contains values.


0
 
leakim971PluritechnicianCommented:
<ul>
<li>0</li>
</ul>

<ul>
<li>1</li>
</ul>

<ul>
<li>2</li>
</ul>

if you have this

each li don't have previous li because all are alone in their ul box
0
 
areyouready344Author Commented:
Each time through the each loop, the html() says null,

alert("check li variable value: " + $("#set li").prev().html());
0
 
areyouready344Author Commented:
Of course that makes sense, thanks leakim...
0
 
areyouready344Author Commented:
its working, thanks for really understanding this issue...
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now