Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
126
rated 0 times [  131] [ 5]  / answers: 1 / hits: 132241  / 14 Years ago, thu, april 29, 2010, 12:00:00

I have a function that checks the age of a form submission and then returns new content in a div depending on their age. Right now I am just using getElementById to replace the HTML content. BUt I think would work better for me if I could also add a class to a div as well. So for example I have.



if (under certain age) {
document.getElementById('hello').innerHTML = <p>Good Bye</p>;
createCookie('age','not13',0)
return false;
} else {
document.getElementById('hello').innerHTML = <p>Hello</p>;
return true;
}


What I would like to do is have everything in a div and if return false then that div disappears and is replaced with other content. Can I get any ideas on a good way to achieve this with pure JavaScript. I don't want to use jQuery for this particular function.


More From » javascript

 Answers
7

If the element has no class, give it one. Otherwise, append a space followed by the new className:



  var el = document.getElementById('hello');
if(el) {
el.className += el.className ? ' someClass' : 'someClass';
}

[#96923] Tuesday, April 27, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
coby

Total Points: 27
Total Questions: 102
Total Answers: 97

Location: Honduras
Member since Wed, Jul 14, 2021
3 Years ago
;