/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Function Problem

Hello – I am having a problem with a JS function I’m trying to write!

The idea is to find all <div>s with the name “alabama[]” ([0] to however many there are) and hide them.

But, I continue to get “Object Required”. Every way I try to pass the argument, I still get the error.

Here is my function:

[code=html]
function showCounty(terSize,terName) {
for(i=0;i<terSize;i++) {
hide = document.getElementById(“alabamacounty0”);
hide.style.visibility = “hidden”;

}
[/code]

Here is what I’m passing:

[code=html]
<a href=”#” onclick=”showCounty(2,’alabamacounty’)”>
Hide Me</a>
[/code]

I’ve tried sending ‘alabamacounty’ with [] at the end and without. Also the same at the top in the function.

My <div>s are as such:

[code=html]
<div id=”alabamacounty[0]”>Test</div>
<div id=”alabamacounty[1]”>Test 2</div>
[/code]

What am I doing wrong?

Thanks!

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Arty_EffemMar 08.2008 — [CODE]function showCounty(terSize,terName)
{
for([B]var[/B] i=0; i<terSize; i++)
{
hide = document.getElementById("alabamacounty["+i+"]");
hide.style.visibility = "hidden";
}
}

/*Or if the indices start at 0 and there are no gaps, you don't need to specify a size*/

function showCounty(terName)
{
for(var i=0; hide = document.getElementById("alabamacounty["+i+"]"); i++)
hide.style.visibility = "hidden";

}[/CODE]
Having said that, I don't think []s are legal in IDs, in which case you could just give them numeric suffixes and address them:
[CODE]document.getElementById("alabamacounty"+i);[/CODE]
Copy linkTweet thisAlerts:
@ts10authorMar 08.2008 — Arty,

Thanks for the help. I noticed a slight error in the script I sent you because I had one of my hard-code tests instead of exactly what I need. I think you're right about the [] in divs and I started out without and couldn't get that to work either!

I have adjusted my script to your suggestions and corrected my error (note: terName in the getElementById as "alabamacounty" is what is being passed) - see below:
[code=html]
function showCounty(terSize,terName) {
for(var i=0; hide = document.getElementById(terName+i); i++) {

hide.style.visibility = "hidden";
alert(hide);
}
[/code]


Here's the HTML:

[code=html]
<a href="#" onclick="showCounty(2,'alabamacounty')">
Hide Me</a>
[/code]


And again, my div change:
[code=html]
<div id="alabamacounty0">Test</div>
[/code]


Alerting "hide" shows "[object]" instead of "alabamacounty0".

Am I still missing something?

Thanks again for your assistance.
Copy linkTweet thisAlerts:
@ts10authorMar 08.2008 — Actually, it works! I don't know why it shows [object], but it worked perfectly. thanks for your help!
×

Success!

Help @ts10 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.15,
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,
)...