/    Sign up×
Community /Pin to ProfileBookmark

Messed up form JAVASCRIPT :(

Hello everyone.
I have a form with a drop down menu that the user uses to select how many name fields will be showing. Currently, it functions by toggling table rows that have the name fields in them from the “display:none” css style to back to normal. This is creating a problem because when a user selects the number of name fields once and then changes their mind and selects another number of name fields, the table rows that are visible become all messed up because the fields that were previously visible are toggled back to “display:none”.

Because of this, I would like to make it so that when a user selects the number of name fields from the drop down menu, instead of toggling the table row, it makes the table rows that will show set to normal, and the other table rows that will not show to a set style of “display:none”, so that there are no jumbled up table rows.

But the only problem with that is, I hardly know anything about javascript, and have no idea how to code that.

Here is the particular portion of my form that I am talking about, along with the javascript I am currently using.

[code=html]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Form document</title>
<script type=”text/javascript”>
function toggleTR(TRid){
if(document.getElementById(TRid).style.display == ‘none’){
document.getElementById(TRid).style.display = ”; // Assign empty string for deafult browser display
}else{
document.getElementById(TRid).style.display = ‘none’;
}
}
</script>
</head>
<body>
<form>
<fieldset>
<legend>Names of Participants</legend>
<label for=”jumpMenu”>Number of people coming:</label>
<select name=”jumpMenu” id=”jumpMenu”>
<option selected=”selected”>-</option>
<option onclick=”toggleTR(‘one’);”>1</option>
<option onclick=”toggleTR(‘one’); toggleTR(‘two’);”>2</option>
<option onclick=”toggleTR(‘one’); toggleTR(‘two’); toggleTR(‘three’);”>3</option>
<option onclick=”toggleTR(‘one’); toggleTR(‘two’); toggleTR(‘three’); toggleTR(‘four’);”>4</option>
<option onclick=”toggleTR(‘one’); toggleTR(‘two’); toggleTR(‘three’); toggleTR(‘four’); toggleTR(‘five’);”>5</option>
<option onclick=”toggleTR(‘one’); toggleTR(‘two’); toggleTR(‘three’); toggleTR(‘four’); toggleTR(‘five’); toggleTR(‘six’);”>6</option>
<option onclick=”toggleTR(‘one’); toggleTR(‘two’); toggleTR(‘three’); toggleTR(‘four’); toggleTR(‘five’); toggleTR(‘six’); toggleTR(‘seven’);”>7</option>
<option onclick=”toggleTR(‘one’); toggleTR(‘two’); toggleTR(‘three’); toggleTR(‘four’); toggleTR(‘five’); toggleTR(‘six’); toggleTR(‘seven’); toggleTR(‘eight’);”>8</option>
</select>
<br />
<br />
<table border=”0″>
<tr id=”one” style=”display:none;”>
<td class=”cooltd”><label for=”name1″>Name:</label></td>
<td><input type=”text” name=”FieldData3″ value=”” maxlength=”100″ size=”30″ id=”name1″ /></td>
</tr>
<tr id=”two” style=”display:none;”>
<td class=”cooltd”><label for=”name2″>Name:</label></td>
<td><input type=”text” name=”FieldData5″ value=”” maxlength=”100″ size=”30″ id=”name2″ /></td>
</tr>
<tr id=”three” style=”display:none;”>
<td class=”cooltd”><label for=”name3″>Name:</label></td>
<td><input type=”text” name=”FieldData7″ value=”” maxlength=”100″ size=”30″ id=”name3″ /></td>
</tr>
<tr id=”four” style=”display:none;”>
<td class=”cooltd”><label for=”name4″>Name:</label></td>
<td><input type=”text” name=”FieldData9″ value=”” maxlength=”100″ size=”30″ id=”name4″ /></td>
</tr>
<tr id=”five” style=”display:none;”>
<td class=”cooltd”><label for=”name5″>Name:</label></td>
<td><input type=”text” name=”FieldData11″ value=”” maxlength=”100″ size=”30″ id=”name5″ /></td>
</tr>
<tr id=”six” style=”display:none;”>
<td class=”cooltd”><label for=”name6″>Name:</label></td>
<td><input type=”text” name=”FieldData13″ value=”” maxlength=”100″ size=”30″ id=”name6″ /></td>
</tr>
<tr id=”seven” style=”display:none;”>
<td class=”cooltd”><label for=”name7″>Name:</label></td>
<td><input type=”text” name=”FieldData15″ value=”” maxlength=”100″ size=”30″ id=”name7″ /></td>
</tr>
<tr id=”eight” style=”display:none;”>
<td class=”cooltd”><label for=”name8″>Name:</label></td>
<td><input type=”text” name=”FieldData17″ value=”” maxlength=”100″ size=”30″ id=”name8″ /></td>
</tr>
</table>
</fieldset>
</form>
</body>
</html>
[/code]

You can see that when you select all eight and then select five it becomes 8 minus 5,or three, instead of five fields like its supposed to.

It would be very helpful if you gave me ready to use html, because I know almost nothing at all about javascript.

Thank you very much!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@phrank80Apr 17.2009 — I made a change to the onclick for each selection you had there and also changed the function itself. Probably not the best code, but hopefully it does what you need it to do.


[code=html]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Form document</title>
<script type="text/javascript">
function toggleV2(QTY){
var maxRows = 8; //max number of inputs/rows you have
for(i = 1; i <= QTY; i++)
{
document.getElementById(i).style.display = ''; // make visible
}

for(i = QTY+1; i <= maxRows; i++)
{
document.getElementById(i).style.display = 'none'; //hide these
}

}
</script>
</head>
<body>
<form>
<fieldset>
<legend>Names of Participants</legend>
<label for="jumpMenu">Number of people coming:</label>
<select name="jumpMenu" id="jumpMenu">
<option selected="selected">-</option>
<option onclick="toggleV2(1);">1</option>
<option onclick="toggleV2(2);">2</option>
<option onclick="toggleV2(3);">3</option>
<option onclick="toggleV2(4);">4</option>
<option onclick="toggleV2(5);">5</option>
<option onclick="toggleV2(6);">6</option>
<option onclick="toggleV2(7);">7</option>
<option onclick="toggleV2(8);">8</option>
</select>
<br />
<br />
<table border="0">
<tr id="1" style="display:none;">
<td class="cooltd"><label for="name1">Name:</label></td>
<td><input type="text" name="FieldData3" value="" maxlength="100" size="30" id="name1" /></td>
</tr>
<tr id="2" style="display:none;">
<td class="cooltd"><label for="name2">Name:</label></td>
<td><input type="text" name="FieldData5" value="" maxlength="100" size="30" id="name2" /></td>
</tr>
<tr id="3" style="display:none;">
<td class="cooltd"><label for="name3">Name:</label></td>
<td><input type="text" name="FieldData7" value="" maxlength="100" size="30" id="name3" /></td>
</tr>
<tr id="4" style="display:none;">
<td class="cooltd"><label for="name4">Name:</label></td>
<td><input type="text" name="FieldData9" value="" maxlength="100" size="30" id="name4" /></td>
</tr>
<tr id="5" style="display:none;">
<td class="cooltd"><label for="name5">Name:</label></td>
<td><input type="text" name="FieldData11" value="" maxlength="100" size="30" id="name5" /></td>
</tr>
<tr id="6" style="display:none;">
<td class="cooltd"><label for="name6">Name:</label></td>
<td><input type="text" name="FieldData13" value="" maxlength="100" size="30" id="name6" /></td>
</tr>
<tr id="7" style="display:none;">
<td class="cooltd"><label for="name7">Name:</label></td>
<td><input type="text" name="FieldData15" value="" maxlength="100" size="30" id="name7" /></td>
</tr>
<tr id="8" style="display:none;">
<td class="cooltd"><label for="name8">Name:</label></td>
<td><input type="text" name="FieldData17" value="" maxlength="100" size="30" id="name8" /></td>
</tr>
</table>
</fieldset>
</form>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@WasifauthorApr 18.2009 — thanks! problem is fixed?
×

Success!

Help @Wasif 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.19,
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,
)...