/    Sign up×
Community /Pin to ProfileBookmark

How do you make 2 different divs to act differently?

This is right in my nose but have gotten to clueless land…anyone know how to make one div work one way and the other another way?

Meaning I have a radio button that when I onclick, I get the div that is hidden….but when I want another function to mouseover(on a different place) I want a different div to open? BUt the way I have my script it works fine with the radio by giving the onclick to : view(this);…

If I want a onmouseover so it can open a div what else do I have to define??

[CODE]
function hideDiv(){
tag = document.getElementsByTagName(“div”);
for(x=0;x<tag.length; x++){
if(tag[x].getAttribute(‘id’).indexOf(“pick_”) != -1){
tag[x].style.display = “none”;
}
}
}
function view(id){
ic = document.getElementById(‘pick_’ + id.value);
hideDiv();
ic.style.display = “block”;
}

[/CODE]

I done a mouseover like this:

[CODE]
function hideDivs(){
var showHide = document.getElementsByTagName(‘div’)
for(var i=0; i<showHide.length;i++){
showHide[i].style.display = (showHide[i].style.display == ‘block’)? ‘none’:’block’;
}
}
[/CODE]

I guess my question is how do I use both?

I tried adding it like this:

[CODE]
function hideDivs(){
var showHide = document.getElementById(‘show’);
for(var i=0; i<showHide.length;i++){
showHide[i].style.display = (showHide[i].style.display == ‘block’)? ‘none’:’block’;
}
}
[/CODE]

But no luck …?

Thanks

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@UltimaterAug 27.2008 — document.getElementsByTagName returns a nodeList while

document.getElementById returns a single element since the method name doesn't specific Element[color=red]s[/color] plural and IDs are supposed to be unique thus no such DOM method exists.

A quick solution without messing with the HTML would be something like this:
<i>
</i>&lt;script type="text/javascript"&gt;
function getElementsById(id){
var findings=new Array(),els=document.getElementsByTagName("*"),l=els.length;
for(var i=0,i&lt;l;i++)if(els[i].getAttribute("id")==id)findings[findings.length]=els[i];
return findings;
}

function hideDivs(){
var showHide = getElementsById('show');
for(var i=0; i&lt;showHide.length;i++)
showHide[i].style.display = (showHide[i].style.display == 'block')? 'none':'block';
}
&lt;/script&gt;
×

Success!

Help @riskmod spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...