I'm making a formbuilder, I would like to change the appearance of for example a heading div. When clicked it should get a border but when another dynamically generated div is clicked the class should be removed and the second clicked div has to get the .active class.
How can I do this with generated divs?
Anyway I found something that works but I still need the If another div is selected, previous div.removeclass and selected div.addclass
This works:
/* Add Class */
$(document).ready(function() {
$( document ).on( 'click', '.HeadingDiv', function () { /* This '.HeadingDiv' could be anything, I need something dynamic here */
$('.HeadingDiv').removeClass('active'); /* This '.HeadingDiv' could be anything, I need something dynamic here */
$(this).addClass('active');
});
});
You can use a single line to add and remove class on a div. Please remove a class first to add a new class.
I had to transform the divs to list items otherwise all my divs would get that class and only the generated ones should get it Thanks everyone, I love this site and the helpful people on it !!!! You can follow the newbie school project at http://low-budgetwebservice.be/project/webbuilder.html suggestions are always welcome :). So this worked for me:
It's all about the selector. You can change your code to be something like this:
Then use this javascript:
The example in a working jsfiddle
See this api about the selector I used: http://api.jquery.com/descendant-selector/
Are you looking something like this short and effective:
http://jsfiddle.net/XBfMV/
you can simply add a general class 'active' for selected div. when a div is clicked, remove the 'active' class, and add it to the clicked div.
In this mode you can find all element which has class active and remove it
try this