I want that part 1
onclick div style changes and part 2
again on click it comes back to normal. I tried doing so but I failed to achieve the part 2
results.
Following is the Javascript code
function abc() { document.getElementById("test").style.color="red"; }
After clicking the test div again, color should come back to defaulr color i.e. black...
The <div> tag defines a division or a section in an HTML document. The <div> tag is used as a container for HTML elements - which is then styled with CSS or manipulated with JavaScript. The <div> tag is easily styled by using the class or id attribute. Any sort of content can be put inside the <div> tag!
Using JavaScript In vanilla JavaScript, you can use the native createElement() method to create an HTML <div> element and the appendChild() method to append the <div> element to another container.
function abc() { var color = document.getElementById("test").style.color; if (color === "red") document.getElementById("test").style.color="black"; else document.getElementById("test").style.color="red"; }
Using jQuery:
$(document).ready(function(){ $('div').click(function(){ $(this).toggleClass('clicked'); }); });
Live example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With