Forum Moderators: open

Message Too Old, No Replies

Modularizing Class Change & Toggle Functions

...and figuring out what (e) is for and if it could be used.

         

JAB Creations

4:39 pm on Apr 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I know I posted something like this but in my research over the past couple days I've noticed (e) being used a lot while I was doing online research. I want to take two basic scripts and remove their code from the XHTML to a JavaScript includes. That part I still haven't figured out on my own though have made plenty of failed attempts thus far.

Secondly I want to make the next version of my site completely keyboard-only accessible (if they do not have a mouse that is) and so I want to make these modular triggers execute on both onclick and onkeypress events. For clarification, the anchor has an id, and when the mouse or key is pressed on that element with the id, it then executes the function to change the targeted id's class.

I have a full fledged test case below. In the last attempt to get a jQuery event to work I had to use a function that is executed at window.onload. I've tried all sorts of combinations again without success to mimic the jQuery events and tried a few things I've found online without success. Guess I need more repetition to pick up this pattern.

Also could someone please explain (e) event? Can I use that or will onblur, onmouseup, onmouseover, and other undesired events trigger this? It's a little hard to reference "e" online through the search engines! ;)

- John

Test Case

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Test Case</title>
<script type="text/javascript">
//<![CDATA[

// Class Changer
function change(id, newClass)
{
identity=document.getElementById(id);
identity.className=newClass;
}

// Class Toggler
function toggle(id, classOne, classTwo)
{
identity=document.getElementById(id);
class_name = identity.className;
if (class_name == classOne) {
identity.className = classTwo;
} else {
identity.className = classOne;
}
}

// Modularized Events
function init28() {
if (document.getElementById('divone')) {
// modularized class changer code goes here
}
if (document.getElementById('divtwo')) {
// modularized class toggler code goes here
}
} window.onload = init28;
////]]>
</script>
<style type="text/css">
a:link, a:visited {color: #fff;}
div {margin: 8px; padding: 8px;}
div.red {background: #a00;}
div.blue {background: #00a;}
</style>
</head>

<body>

<div id="divone" class="red">
<a href="#" id="anchor1" onclick="change('divone', 'blue');">Class Change To Blue</a><span> - </span><a href="#" id="anchor2" onclick="change('divone', 'red');">Class Change To Red</a>
</div>

<div id="divtwo" class="red">
<a href="#" id="anchor3" onclick="toggle('divtwo', 'blue','red');">Toggle between Red and Blue</a>
</div>

</body>
</html>

Fotiman

7:33 pm on Apr 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Some libraries (Yahoo UI Library for instance) will pass a reference to the event that triggered the callback as the only argument to the callback. This is usually passed as a variable "e". Since IE handles events differently than Mozilla-based browsers, the libraries do all of the heavy lifting for you (so you don't have to do that work yourself).

For example, you might have something like this with the Yahoo Event Utility:


function myCallback(e) {
// e, in this case, represents the event that triggered
// this callback method. In this case, it's the
// window.onload event.
}
YAHOO.util.Event.on(window,'load',myCallback);

I think this is probably what you're seeing.

JAB Creations

7:49 pm on Apr 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well that explains the "e".

I can't even get something like element.click {alert ('');} to work in Firefox for basic click testing!

- John

JAB Creations

5:43 pm on Apr 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I screw this up it's because I keep trying to do an init function where I simply need to do window.onload = function() {} to surround the modularized events. Anyway this works perfectly and helps my understanding of JavaScript improve.

- John

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Test Case</title>
<script type="text/javascript">
//<![CDATA[

// Class Changer
function change(id, newClass)
{
identity=document.getElementById(id);
identity.className=newClass;
}

// Class Toggler
function toggle(id, classOne, classTwo)
{
identity=document.getElementById(id);
class_name = identity.className;
if (class_name == classOne) {
identity.className = classTwo;
} else {
identity.className = classOne;
}
}

// Modularized Events
window.onload = function() {
document.getElementById("anchor1").onclick = function() {
change('divone', 'blue'); return false;
}
document.getElementById("anchor2").onclick = function() {
change('divone', 'red'); return false;
}
document.getElementById("anchor3").onclick = function() {
toggle('divtwo', 'blue','red'); return false;
}
}

////]]>
</script>
<style type="text/css">
a:link, a:visited {color: #fff;}
div {margin: 8px; padding: 8px;}
div.red {background: #a00;}
div.blue {background: #00a;}
</style>
</head>

<body>

<div id="divone" class="red">
<a href="#" id="anchor1" onclick="change('divone', 'blue');">Class Change To Blue</a><span> - </span><a href="#" id="anchor2" onclick="change('divone', 'red');">Class Change To Red</a>
</div>

<div id="divtwo" class="red">
<a href="#" id="anchor3" onclick="toggle('divtwo', 'blue','red');">Toggle between Red and Blue</a>
</div>

</body>
</html>