/    Sign up×
Community /Pin to ProfileBookmark

Show/Hide DIV’s Generated Dynamically

Ok, I am very new to JS, but I have had a fair bit of experience in PHP, ASP, .NET (<- bah), pearl and a few others so I know coding concepts ?

Here is my problem: I have an external JS file that I am loading an array from (places[]). I have a form on the page that creates checkboxes from the array as follows:

[CODE] <tr><td>
Place(s) :
</td><td>
<script language=Javascript>
<!–
//<![CDATA[
for(var i=0; i<=places.length-1; i++) {
document.write(“<input type=”checkbox” name=””+places[i]+”” onClick=”HideUnhide(“+places[i]+”Div)”>”+places[i]+”</input>”);
}
//]]>
//–>
</script>
</td></tr>
[/CODE]

The onClick handler that I have put in there is where the problem starts. It is MEANT to display various DIV tags that I have made invlisible like so:

[CODE]
<div id=’BankstownDiv’ style=”display: none;”>
<h2>Bankstown Lecturer</h2>
Same As Unit Coordinator:
<input type=”checkbox” name=”Bankstown” id=”Bankstown” />
Name : <br />
<input type=”text” name=”CLName” /><br />
Location – <br />
Room Number : <br />
<input type=”text” name=”CLRoom” /> <br />
Building :<br />
<input type=”text” name=”CLBuilding” /><br />
</div>
etc…..
[/CODE]

And this is the Javascript function that I have placed in the HEAD of the doco:

[CODE]
function HideUnhide(objectID)
{
if(document.getElementByID(objectID).style.display == ‘none’ ) {
document.getElementById(objectID).style.display = ‘block’;
}
else{
document.getElementById(objectID).style.display = ‘none’;
}
}
[/CODE]

Now when I test it in IE6 I get an error “Object doesn’t support this property or method” and it points me to the following line of code:

[CODE]
function HideUnhide(objectID)
{
[B][U]if(document.getElementByID(objectID).style.display == ‘none’ )[U][B] {
document.getElementById(objectID).style.display = ‘block’;
}
else{
document.getElementById(objectID).style.display = ‘none’;
}
}
[/CODE]

As far as I was aware, the DIV element (which I am referring to, for example, the value for place[2]=”Bankstown” and I simply refer to its DIV tag –which is called BankstownDiv — by stating place[2]+”Div”) DOES have a style.display method…at least that what some JS tutes have indicated, and I can get the DIV’s initially hidden using the style=”display:none;” attribute but I simple can not work out why this script wont run successfully.

Any help would be GREATLY appreciated!! ?

Thanks in advance fellas!

-cptn_spoon

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@PittimannMay 03.2004 — Hi!

In your if statement, you have a spelling mistake:

if(document.getElementByI[COLOR=red]D[/COLOR](objectID).style.display == 'none' ) {



The red "D" has to be lower case (like in the lines following in your code)...



Cheers - Pit
Copy linkTweet thisAlerts:
@cptn_spoonauthorMay 03.2004 — I just realised that as well, thanks Pittiman, BUUUT, when I fix that error up I am now back to my ORIGINAL error

"OBJECT REQUIRED"

I really dont understand this now...

Thanks for you help!
Copy linkTweet thisAlerts:
@PittimannMay 03.2004 — Hi!

Sorry! When seeing what I already pointed out, I didn't look deeper at the rest :rolleyes:

You're missing quotes in the function call. You are passing a string argument and that has to be quoted:

document.write("<input type="checkbox" name=""+places[i]+"" onClick="HideUnhide([COLOR=tomato]'[/COLOR]"+places[i]+"Div[COLOR=tomato]'[/COLOR])">"+places[i]);



Cheers - Pit
Copy linkTweet thisAlerts:
@JayDieMay 03.2004 — Think the problem is in this line:
document.write("&lt;input type="checkbox" name=""+places[i]+"" onClick="HideUnhide("+places[i]+"Div)"&gt;"+places[i]+"&lt;/input&gt;");
When you call the function, you give the [u]id-name[/u] of your DIV. But... you don't quote them?!
onClick="HideUnhide(" "+places[i] +"Div")"&gt;
Hope this will solve your problem...
Copy linkTweet thisAlerts:
@JayDieMay 03.2004 — Damn Pittimann ? I saw it first ? ?
Copy linkTweet thisAlerts:
@PittimannMay 03.2004 — Hi!

Shall I delete my post?? I might do so, but only if you change the new quotes to single ones :p

Cheers - Pit
Copy linkTweet thisAlerts:
@JayDieMay 03.2004 — [i]Originally posted by Pittimann [/i]

[B]...but only if you change the new quotes to single ones :p[/B][/QUOTE]

You're right! No, you don't have to delete your post! Little mistake of me, quotes... it's a little messy there ?
Copy linkTweet thisAlerts:
@cptn_spoonauthorMay 03.2004 — haha, such an enthusiastic bunch!!

Thanks a lot fellows, it is all working fine now. And just for the record, you both get my best wishes (if they're worth anything... ? )

Now back to trying to learn Javascript!
×

Success!

Help @cptn_spoon 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.17,
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,
)...