I have two functions: one creates paragraphs dynamically, and the other function selects ("changes the background color") and deselects ("changes to the default background color"). The problem is that I can select all paragraphs at once, and I want to be able to select one paragraph at a time, of course, after deselecting the already selected paragraph.
How to prevent selection / selection of another paragraph when it is already selected / highlighted?
Here you can check the full source code: http://jsfiddle.net/2QqmN/1/
JQuery Code:
$(document).ready(function(){
var count = 1;
$("#add").on(
"click", function(){
$("#a").append('<p>Paragraph ' + count + '</p>');
count++;
});
$("#a").on("click", "p", function(){
var bg = $(this).css("background-color");
if(bg=="rgb(255, 255, 255)") {
$(this).css({"background-color":"green", "color":"white"});
}
else {
$(this).css({"background-color":"white","color":""});
}
});
});
If I were clear enough, please help! You will be appreciated!
source
share