Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

changing the classname of a div

Posted on 2008-06-09
4
Medium Priority
?
1,420 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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 a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn how to dynamically set the form action using jQuery.

670 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