/    Sign up×
Community /Pin to ProfileBookmark

Using Javascript with form

Hi,

I have some code that isn’t working quite like I expect. I have a simple drop down menu with options, and I want to make it so that when the end user selects an option it turns the visibility of a div on or off. Eventually I am going to have 150 or so options but here is what I have so far:

[CODE]
<body>
<form id=”form1″ name=”form1″ method=”post”>
Card
<select name=”select”>
<option>Choose Code</option>
<option>AAA</option>
<option>BBB</option>
<option>CCC</option>
<option>DDD</option>
</select>
<input type=”submit” name=”Submit” value=”Submit” onclick=”document.getElementById(‘AAA’).style.display == ‘block’ ?
document.getElementById(‘AAA’).style.display = ‘none’:
document.getElementById(‘AAA’).style.display = ‘block’; return true;”/>
</form>
<div id=”AAA” style=”DISPLAY: none”>AAA cards are good.</div>
<div id=”BBB” style=”DISPLAY: none”>BBB cards are good.</div>
<div id=”CCC” style=”DISPLAY: none”>CCC cards are good.</div>
<div id=”DDD” style=”DISPLAY: none”>DDD cards are good.</div>
</body>
[/CODE]

What is strange is that this works intermittently. If I test it and I choose “AAA” and submit, I get nothing. Then repeat with “BBB” and nothing. Then “CCC” and my AAA div then appears and quickly disappears.

I also want the divs to load dynamically, so if you choose “AAA” you see the AAA div, if you choose “BBB” you see the BBB div, and so on.

Is it possible to this working with javascript only, no database, can’t touch the <head> or <body> tag?

Thanks

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@KDLADec 29.2008 — This is just a suggestion, but why don't you do something like this: http://javascript.internet.com/miscellaneous/content-display.html

but put it in the select and onchange?
Copy linkTweet thisAlerts:
@craigwgauthorDec 29.2008 — I wish I could use that idea. However I can't touch the <head> tag, nor can I use linked javascript, or even embedded javascript. Given my limitations with the server all I can use is embedded script, which hinders my little aspirations, but I have lofty goals.

I actually use a similar idea where I only have 3 choices on another page, but for this page there are going to be 150+ options and I can't display them all as clickable links and keep any semblence of organization at the same time.

Any other ideas?

Craig
Copy linkTweet thisAlerts:
@KDLADec 29.2008 — Nope - not if you can only do "inline" stuff. Wow - 150+ lines of HTML/JS for this.... ?

Good Luck!

KDLA
Copy linkTweet thisAlerts:
@craigwgauthorDec 29.2008 — This is the example I got to work great. The end user just clicks on a link or a button and walla! I just can't figure out how to cram it into a drop down menu.

[CODE]
<body>
<P>Click the text below to find the answer to each question. Click again to close the section. If you want to start over, click "Refresh".</P>
<!-- In adding new sections, the id number, i.e. id="para#", must be different in each one in both the div and the link -->
<BR>
<TABLE width="100%" border=1>
<TBODY>
<TR vAlign=top>
<TD width="16%"><A onclick="document.getElementById('para1').style.display == 'block' ? document.getElementById('para1').style.display = 'none': document.getElementById('para1').style.display = 'block'; return true;" href="#">What is the value of pi?</A> </TD>
<TD width="84%">&nbsp;
<DIV id=para1 style="DISPLAY: none">3.14
<P>
<INPUT class=button onclick="document.getElementById('para2').style.display == 'block' ? document.getElementById('para2').style.display = 'none': document.getElementById('para2').style.display = 'block'; return true;" type=button value="What are the next two digits?" name=button1>
</P>
<P>
<DIV id=para2 style="DISPLAY: none">15</DIV>
<P>
<INPUT class=button onclick="document.getElementById('para3').style.display == 'block' ? document.getElementById('para3').style.display = 'none':document.getElementById('para3').style.display = 'block'; return true;" type=button value="What are the next two digits after that?" name=button1>
</P>
<P>
<DIV id=para3 style="DISPLAY: none">92</DIV>
</DIV></TD>
</TR>
</TBODY>
</TABLE>
<DIV id=Shipping style="DISPLAY: none">&nbsp;</DIV>
</div>
</body>
[/CODE]


If I could come up with a better way to summarize the options that would work too. Like, have the end user manually type it in an input field? But either way its a form, so I'm stuck. Does that help expand ideas?

Craig
×

Success!

Help @craigwg 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...