Reputation:
Hi I have a json object with which I am creating unordered list of elements.It's pretty straightforward.
So far I have this simple script.
var data_to_send = $("#form1>div").find('input[name = "txt1"]').val();
var data = ["Argentina", "Uk", "USA"];
var obj = $("#div1");
obj.append("<ul class='outerul'>");
$.each(data, function (index, value) {
obj.append("<li class='outerclass'>" + value + " : " + index + "</li>");
});
obj.append("</ul>");
$('ul.outerul').css('list-style-type', 'square'); ??How can I apply style here
Now I am trying to apply style to li element which I am not able.Also any idea how to toggle the elements on click.
Upvotes: 1
Views: 4455
Reputation: 5962
here is your JS
var data = ["Argentina", "Uk", "USA"];
var obj = $("#div1");
obj.append("<ul class='outerul'>");
$.each(data, function (index, value) {
obj.append("<li class='outerclass'>" + value + " : " + index + "</li>");
});
obj.append("</ul>");
$('li.outerclass').css('list-style-type', 'square');
Upvotes: 0
Reputation: 2273
You can acheive this in multiple ways using jQuery / CSS.
JQUERY Option:
$('ul li').css('list-style-type','square');
Fiddle: http://jsfiddle.net/kiranvarthi/vm0tjn6a/
CSS Option:
ul li { list-style-type: square; }
Upvotes: 0
Reputation: 388446
The problem is the li
s are added to div
not ul
. obj
is the #div1
element not the ul.outerclass
element so the style applied to ul.outerclass
is not getting applied to the li
s added to obj
//var data_to_send = $("#form1>div").find('input[name = "txt1"]').val();
var data = ["Argentina", "Uk", "USA"];
var $obj = $("#div1");
var $ul = $("<ul class='outerul'>").appendTo($obj);
$.each(data, function(index, value) {
$ul.append("<li class='outerclass'>" + value + " : " + index + "</li>");
});
$('ul.outerul').css('list-style-type', 'square');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="div1"></div>
But as a more appropriate solution use a class to style the elements
//var data_to_send = $("#form1>div").find('input[name = "txt1"]').val();
var data = ["Argentina", "Uk", "USA"];
var $obj = $("#div1");
var $ul = $("<ul class='outerul'>").appendTo($obj);
$.each(data, function(index, value) {
$ul.append("<li class='outerclass'>" + value + " : " + index + "</li>");
});
.outerclass {
list-style-type: square;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="div1"></div>
Upvotes: 3
Reputation: 11681
To add/toggle css class on click you can add this at the end of your code:
//var data_to_send = $("#form1>div").find('input[name = "txt1"]').val();
var data = ["Argentina", "Uk", "USA"];
var $obj = $("#div1");
var $ul = $("<ul class='outerul'>").appendTo($obj);
$.each(data, function(index, value) {
$ul.append("<li class='outerclass'>" + value + " : " + index + "</li>");
});
$('ul.outerul').css('list-style-type', 'square');
$ul.click(function() {
$(this).toggleClass( 'redText');
});
.redText {
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="div1"></div>
click on list to change color
Upvotes: 0