Solved

changing the classname of a div

Posted on 2008-06-09
4
1,409 Views
Last Modified: 2013-11-19
Hi There,

I'm trying to use javascript to change the class name of an object (div) with onmouseover and onmouseout events. I tried the code below which seems to work fine in IE and Safari but FF is returning an error:

ctl00_mainContent_1 is not defined

Can someone post some cross browser code that will perform a similar function without giving me an error?

Sean - thanks in advance

!----- js code
 
function setClassName(objId, className) {
    	document.getElementById(objId.getAttribute("id")).className = className;
}
 
--html code
 
        <div id="ctl00_mainContent_1" class="thumbs"><a href="#" onclick="NextPreviouspage(1);" onmouseover="setClassName(ctl00_mainContent_1,'thumbsGlow');" onmouseout="setClassName(ctl00_mainContent_1,'thumbs');"><img src="images/photography/thumb_1.jpg" border="0" /><a/></div>

Open in new window

0
Comment
Question by:MrAgile
4 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 21749022
Try like this

 function setClassName(objId, className) {
          document.getElementById(objId).className = className;
}

</script>
<style>
.x
{
 background-color:Aqua;
}

.y
{
 background-color:Lime;
}
</style>

<div id="div1" class="x" onmouseover="setClassName('div1','y')">
     
0
 
LVL 21

Accepted Solution

by:
naspinski earned 500 total points
ID: 21749062
youw ill need to be a bit more thorough to cover all browsers
var newObject = document.getElementById(objId)
newObject.setAttribute('className', 'name_of_css_class');// for IE
newObject.setAttribute('class', 'name_of_css_class');// for others (these are both needed)

Open in new window

0
 
LVL 9

Expert Comment

by:zemond
ID: 21749083
onmouseover="setClassName(ctl00_mainContent_1,'thumbsGlow');"

turn into

onmouseover="this.className = 'thumbsGlow'"

Should be fine in modern browsers
0
 
LVL 8

Author Comment

by:MrAgile
ID: 21755725
Hi All,

Thanks for the answers. naspinski's answer was the one that worked in all browsers. I appreciate everyone's help.


Sean
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this tutorial viewers will learn how to style different bullet points for unordered lists in CSS. Begin with a normal unordered list; the default bullet point is a solid circle: In the CSS, create a defined class of unordered list by typing "ul.S…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question