I have a number of lists, and want to toggle the css (background) of this li element when this is clicked launching a popup, the popup is working but cant get the on/off toggle to work which changes the background color of the item clicked.
here is the code below// its at the bottom the main part of the question- if any one can let me know how the above could be acheived that would be great + much appreciated
function displayChannels() {
$.each(channel_list, function (index, channel) {
//Make the code for the channel
var code_channel = "<li><img src='" + channel._icon + "'/></li>";
//Display it
$("#channel-list").append(code_channel);
//Open a list for the channel's program list
var code_prog_list = "<ul class='channel_row'>";
//Add each program in it as a li each time
$.each(channel._programs, function (index, programme) {
code_prog_list += "<li><div class='text-left'>" + "<h5>" + programme._hour + ":" + programme._minutes + "</h5>" + "<h6>" + programme._title + "</h6>" + "<p>" + programme._cat + " (" + programme._duree + "mn)" + "</p></div>" + "<div class='programme_icon'><img src='" + programme._icon + "'' alt=''/></div>" + "<div class='desc'>" + programme._desc + "</div>" + "<div class='star'>" + programme._star + "</div>";
code_prog_list += "</li>";
//$(this).children().width(programme._width);
//$(this).css( "width", programme._width + 'px');
//$( "#channel_row li").width(programme_width);
//$(this).children('.channel_row li').width(programme._width);
});
//Close the list
code_prog_list += "</ul>";
//Display it
$("#prog-grid").append(code_prog_list);
$("#prog-grid ul").last().resizePrograms();
});
$("#loader").hide();
scrollTime();
$(".channel_row li").on("click", showPopup);
$("#popup").on("click", function () {
$("#popup").toggle(500);
});
}
function showPopup() {
var x = "<div id='close_button'></div>";
$("#popup").html(x).show(500);
$(this).children().css("background", "red");
}
Add a class, then remove and add as needed -
function showPopup() {
var x = "<div id='close_button'></div>";
$("#popup").html(x).show(500);
$('li').removeClass('redClass'); // remove those with the class first
$(this).children().addClass('redClass'); // add the class to the current selected
}
You could try using the handy toggleClass function on the LI itself and leave CSS to figure out how to handle the background colors?
See example here: http://jsfiddle.net/mmJy3/
$(this).toggleClass("onecolor");
I used your code and created a sample Fiddle Here
As stated above, you just need to remove the other background colors and set the current one to what you want. I changed your last function to this:
function showPopup() {
var x = "<div id='close_button'></div>";
$("#popup").html(x).show(500);
$(".channel_row li").children().css('background-color','');
$(this).children().css('background-color','red');
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.