Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

changing the classname of a div

Posted on 2008-06-09
4
Medium Priority
?
1,425 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

885 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