Debugging JavaScript & css code: adding an event handler

macsplean

You can see the code here: http://jsfiddle.net/KfwyL/ I have a div and inside of the div there is an h1. I have the h1 set so that on hover it becomes green. I want to make it so that when the mouse hovers over the h1, the div gets a box shadow. my code is not working.

HTML:

<!DOCTYPE html>
<head> 
<link rel="stylesheet" type="text/css" href="../stylesheets/1.css"> 
<title> Max Pleaner's First Website
</title>
</head>
<body>
  <div class="welcomebox"> 
    <h1 class="welcometext">Welcome to my site.
    </h1> 
    </div>
</body>
<<script src="../Scripts/1.js"> </script>
</html>

css:

body {
    background-image:url('../images/sharks.jpg');
    }

.welcomebox {background-color:#1C0245;
  -webkit-border-radius: 18px;
  -moz-border-radius: 18px;
  border-radius: 18px;
  width: 390px;
  height: 78px;
    margin-left:100px;
    margin-top:28px;
    border-style:solid;
  border-width:medium;
}

h1 {
    display:inline-block;
    margin-left: 12px;
    height: 40px;
    width: 357px;
    background-color: #670715;
    padding: 4px;
    position: relative;
    bottom: 5px;
    -webkit-border-radius: 14px;
  -moz-border-radius: 14px;
  border-radius: 14px;
}

h1:hover {background-color: green;}

Javascript:

welcomeboxshadow = document.getElementsByClass("welcometext");

function doit()
{
var topbox = document.getElementsbyClass("welcomebox")
topbox.style.box-shadow = "-webkit-box-shadow: 0px 0px 30px rgba(114, 220, 215, 1);-moz-box-shadow: 0px 0px 30px rgba(114, 220, 215, 1);box-shadow: 0px 0px 30px rgba(114, 220, 215, 1);"
};
welcomeboxshadow.onmouseover.doit;
Stephen Byrne

here is a working version of your code that doesn't use jQuery since I figured you wanted to know how to do this in pure JS...

welcomeboxshadow = document.getElementsByClassName("welcometext");
welcomeboxshadow[0].addEventListener('mouseover', 
                                  function() {
                                      var topbox = document.getElementsByClassName("welcomebox");
 topbox[0].setAttribute("class","welcomebox welcomeBoxMouseOver")
                                  }, false)

I changed the inline style to a class but the concept is the same.

The problems were mostly invalid function names (getElementsByClass*Name*), trying to set properties that didn't exist (topbox.style.box-shadow)

Also you need to remember that function returns a collection, not a single element, so you need to reference it using [0]

Note that I would recommend not using the raw js approach in this case, I'd prefer to use jQuery as it's much cleaner and once you go beyond anything simple like your code you will be glad you used it

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

JavaScript click event handler not fired in the code below

From Dev

Execute javascript code at the end of event handler

From Dev

Adding an Event Handler for a List

From Dev

Adding class on hover event handler

From Dev

Moodle 3.1- Event Handler Debugging

From Dev

Overload event handler in javascript

From Dev

Javascript event handler is not working

From Dev

event handler in javascript

From Dev

javascript remove event in handler,

From Dev

Unit test the code of an event handler

From Dev

Syntax for adding an event handler in VB.NET

From Dev

Adding event handler to feature in OpenLayers 3?

From Dev

Adding window.event handler to typescript

From Dev

Adding an event handler inside a knockoutjs custom binding

From Dev

WPF C# adding event handler programmatically

From Dev

Semi-automatically adding event handler methods

From Dev

React Event Handler. Adding values to array

From Dev

Adding event handler to dynamically added span elements

From Dev

Dynamically adding event handler to select box

From Dev

Extend Label control - adding a Click event handler

From Dev

WPF - Adding a handler for PreviewMouseLeftButtonDown suppresses click event

From Dev

adding event handler to a dynamically created button

From Dev

Change event fires jquery handler but not javascript handler

From Dev

Javascript removing Event and adding Event

From Dev

Range Slider Event Handler Javascript

From Dev

Using this in event handler in strict javascript?

From Dev

Event Handler Namespace in Vanilla JavaScript

From Dev

Javascript event handler (mouseover) not firing

From Dev

JavaScript button event handler not working

Related Related

HotTag

Archive