473,385 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Need help approaching / tackling this functionality

OK... i think this is an easy question.

I Have a number of Divs, and when you rollover the divs their border
color changes.

When You click on a div, I want the rollover state to stick.

I still want you to be able to rollover the OTHER divs

If you click on ANOTHER div, I want the first div to go back to its
normal state, and the newly clicked div to get the rollover effect.

There are going to be many, many divs... so I really dont know what is
the smoothest way to go about this!

Any help would be appreciated!

Aug 17 '06 #1
9 1213
ja***********@gmail.com wrote:
OK... i think this is an easy question.

I Have a number of Divs, and when you rollover the divs their border
color changes.

When You click on a div, I want the rollover state to stick.

I still want you to be able to rollover the OTHER divs

If you click on ANOTHER div, I want the first div to go back to its
normal state, and the newly clicked div to get the rollover effect.

There are going to be many, many divs... so I really dont know what is
the smoothest way to go about this!

Any help would be appreciated!
You should store the clicked div in a variable, using the onClick event
and insert code in your rollover function so that it doesn't change the
border color back if that's a selected div.
Aug 17 '06 #2
>From a pseudocode standpoint you would have something like the
following:

var divs = new Array();
divs.push( document.getElementById( 'div1' ) ); // because I can't
remember array literals
divs.push( document.getElementById( 'div2' ) );
divs.push( document.getElementById( 'div3' ) );

var clicked;

function clearAll() {
clicked = false;
for( i in divs ) clear( divs[i] );
}
function clear( theDiv ) {
if( clicked != theDiv ) theDiv.style.className = 'divCleared';
}
function select( theDiv ) { theDiv.style.className = 'divSelected'; }
function choose( theDiv ) {
if( clicked == theDiv ) clearAll();
else {
clearAll();
clicked = theDiv;
select( theDiv );
}
}
....
<div id="div1" onmouseover="select( this );" onmouseout="clear( this
);" onclick="choose( this );"></div>... for each div just change the id

Now for the learning part:
You have several functions here for convenience's sake. Two
concentrate on getting in and out of the rollover state (select &
clear), and two that handle the sticky rollover (choose & clearAll).
The sticky functions keep track of which div is selected, and execute
the non-sticky functions accordingly.

Aug 18 '06 #3
So I liked both answers, but the second one, although probably more
"right", doesnt seem to be working for me isnce im such an amateur...

But the idea of simply adding making a variable that says "dont
rollover if this one has been clicked" sounds great! Its easy for me to
understand, and smells like it will work... but hasnt yet.

here is what im doing:

// Making these global variables
var model;
var theDiv;

// Storing the selected phone into a hidden field for a form
function selectPhone(model) {
document.getElementById('myPhone').value=model;
}

// Making the div that is provided in this paramater have a border
function rollBorder(theDiv) {
document.getElementById(theDiv).style.border='2px solid #ff9900';
document.getElementById(theDiv).style.padding='0px ';
}
// Heres the problem, i want the rolloff only to work if theDiv has not
been clicked

function rollNoBorder(theDiv) {
if (theDiv != model)
{
document.getElementById(theDiv).style.border='0px solid #ff9900';
document.getElementById(theDiv).style.padding='2px ';
}
}


and then here is the div itself:

<div id="box1" onClick="selectPhone('A1000');"
onMouseOver="rollBorder('box1');"
onmouseout="rollNoBorder('box1');">PHONE</div>
Any ideas??

Aug 18 '06 #4

ja***********@gmail.com wrote:
So I liked both answers, but the second one, although probably more
"right", doesnt seem to be working for me isnce im such an amateur...

But the idea of simply adding making a variable that says "dont
rollover if this one has been clicked" sounds great! Its easy for me to
understand, and smells like it will work... but hasnt yet.

here is what im doing:

// Making these global variables
var model;
var theDiv;

// Storing the selected phone into a hidden field for a form
function selectPhone(model) {
document.getElementById('myPhone').value=model;
}

// Making the div that is provided in this paramater have a border
function rollBorder(theDiv) {
document.getElementById(theDiv).style.border='2px solid #ff9900';
document.getElementById(theDiv).style.padding='0px ';
}
// Heres the problem, i want the rolloff only to work if theDiv has not
been clicked

function rollNoBorder(theDiv) {
if (theDiv != model)
{
document.getElementById(theDiv).style.border='0px solid #ff9900';
document.getElementById(theDiv).style.padding='2px ';
}
}


and then here is the div itself:

<div id="box1" onClick="selectPhone('A1000');"
onMouseOver="rollBorder('box1');"
onmouseout="rollNoBorder('box1');">PHONE</div>
Any ideas??
Here's another approach, a slight variant to what you already have. I
think it's somewhat cleaner.

Your CSS:

..borderon
{
border: 2px solid #F90;
padding: 0px;
}

..borderoff
{
border: 0px solid #F90;
padding: 2px;
}

Your JS:

//global variable to keep track of the clicked div
var clicked_div = null;

function myClick(div_elem, model)
{
if(clicked_div)
{
//turn 'off' old div
clicked_div.className = "borderoff";

//replace old div with new clicked div
clicked_div = div_elem;

//turn the new div 'on'
clicked_div.className = "borderon";
}
}

function myMOver(div_elem)
{
div_elem.className = "borderon";
}

function myMOut(div_elem)
{
div_elem.className = "borderoff";

//whatever div element you mouse out of, turn the clicked element on
anyway
if(clicked_div)
{
clicked_div.className = "borderon";
}
}

Your HTML:

<div onclick = "myClick(this, 'A1000');"
onmouseover = "myMOver(this);"
onmouseout = "myMOut(this);">text</div>

One of the benefits of this way is that you can dynamically assign all
your necessary divs the onmouseover and onmouseout event handlers
through javascript instead of handcoding them.

Aug 18 '06 #5
Sounds right, but it seems not to be working for me... the click action
doesnt seem to have any effect. Perhaps its just the Monday morning
idiocy thats keeping me from figuring it out, but is there anything
that is missing in that code?

Aug 21 '06 #6
ja***********@gmail.com said the following on 8/21/2006 11:08 AM:
Sounds right,
What sounds right?
but it seems not to be working for me... the click action
doesnt seem to have any effect.
Then you did something wrong.
Perhaps its just the Monday morning idiocy thats keeping me
from figuring it out, but is there anything that is missing
in that code?
Is it also the Monday morning idiocy that kept you from quoting what
you were replying to?

P.S. The issue with the code, as posted, is double periods in the CSS
definitions. Instead of:

...borderon

It should be:

..borderon

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 21 '06 #7
What sounds right?
The solution just posted... what, you thought i meant the new outkast
album?
Then you did something wrong.
Perhaps, but thats why I was asking for clarifications, because I
pretty much just copied and pasted. Im going to try again now.
Is it also the Monday morning idiocy that kept you from quoting what
you were replying to?
No actually it was the assumption that if your smart enough to answer
my question, then you would be smart enough to follow the conversation.
P.S. The issue with the code, as posted, is double periods in the CSS
definitions. Instead of:

..borderon

It should be:

.borderon
I dont see any double periods in the first place. Thanks though.

Aug 21 '06 #8
As far as I can tell from the code below, clicked_div is defined as
Null in the beginning, but I dont see where that variable is re-defined
with the actual name of the Div that is being clicked.

It seems that the TWO uses of the if statement:
if(clicked_div)

would both return null, and therefore the function will not work
correctly.

Am I missing something?

Any help would be appreciated!


//global variable to keep track of the clicked div
var clicked_div = null;

function myClick(div_elem, model)
{
if(clicked_div)
{
//turn 'off' old div
clicked_div.className = "borderoff";

//replace old div with new clicked div
clicked_div = div_elem;

//turn the new div 'on'
clicked_div.className = "borderon";
}

}

function myMOver(div_elem)
{
div_elem.className = "borderon";

}

function myMOut(div_elem)
{
div_elem.className = "borderoff";

//whatever div element you mouse out of, turn the clicked element on
anyway
if(clicked_div)
{
clicked_div.className = "borderon";
}

}

Aug 21 '06 #9

ja***********@gmail.com wrote:
As far as I can tell from the code below, clicked_div is defined as
Null in the beginning, but I dont see where that variable is re-defined
with the actual name of the Div that is being clicked.
It is generic enough such that you don't need to keep track of the
name. Instead we'll be keeping track of the "object" that represents
the div element.

//global variable to keep track of the clicked div
var clicked_div = null;

function myClick(div_elem, model)
{
if(clicked_div)
{
//turn 'off' old div
clicked_div.className = "borderoff";

//replace old div with new clicked div
clicked_div = div_elem;

//turn the new div 'on'
clicked_div.className = "borderon";
}

}
My apologies. I had it in mind, but forgot to put it in. Place an
else statement, and then assign the div element to the variable. This
should only occur once.

else
{
clicked_div = div_elem;
}

Aug 21 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Jeff Wagner | last post by:
I am in the process of learning Python (obsessively so). I've been through a few tutorials and read a Python book that was lent to me. I am now trying to put what I've learned to use by rewriting...
0
by: MarionEll | last post by:
XML 2004 Deadlines Approaching - Early Bird Registration, Late-Breaking/Product Submissions The IDEAlliance XML 2004 Conference & Exposition http://www.xmlconference.org takes place November...
5
by: Arpan | last post by:
An ASP application retrieves records from a SQL Server database. In the first page of the application, the user has to enter a password & the columns retrieved from the DB table depends upon the...
19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
10
by: pcbutts1 | last post by:
Yes this is a homework assignment. My instructor has broken english and I just could not follow along last night. How do I do this Write the program in Java (without a graphical user interface)...
0
by: trev85852 | last post by:
Im a bit of a VB noob but hey we all gotta start as a noob right? I need a VB script for Outlook that reads in a file and then changes the ntfs file attribute "Title" to the same as the file name....
13
by: Ikke | last post by:
Hi everybody, I've downloaded a design from www.oswd.org and have started rewriting the css and html to end up with a new design for my site. So far so good, but there are a few problems I...
5
by: Paul M | last post by:
Hi I need to create a map for a room (chairs, bed, TV, forniture, etc). All object allready exists like png images. I need to create a map for each room, i mean the user need to create. How...
2
by: MK | last post by:
Hello, I am new to XML and PERL and I have a few questions the answers to which I need to complete a project. All your time and effort would be highly appreciated. I have to make a small HTML page...
22
by: Ramon F Herrera | last post by:
My goal is to study (in the RMS sense) and familiarize myself with some OSS code, until I reach the point at which I can make non-trivial modifications to it. The class of applications I have in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.