/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] dropdown box not showing up in the correct part of the table

Hi all,

I have this code below that displays or hides a dropdown box depending on what is chosen in the drop down box above it. It does work properly. The only problem is that when the 2nd dropdown box is displayed (because the correct option was chosen in the 1st dropdown box), it is not displayed in the correct part of the table. The second dropdown box should be displayed right below the first dropdown box, but instead the 2nd dropdown box is diplayed below the text. When “yes” is chosen from the first dropdown box, the 1st dropdown box also moves to the right. I would like it to stay where it is. Does anyone have any ideas?

Thanks,
Cedric

<script type=”text/javascript”>
// <![CDATA[
function display(obj,id1,id2) {
txt = obj.options[obj.selectedIndex].value;
document.getElementById(id1).style.display = ‘none’;
document.getElementById(id2).style.display = ‘none’;
if ( txt.match(id1) ) {
document.getElementById(id1).style.display = ‘block’;
}
if ( txt.match(id2) ) {
document.getElementById(id2).style.display = ‘block’;
}
}
// ]]>
</script>

<table>
<tr>
<td width=”85″><font size=”2″ face=”Verdana, Arial, Helvetica, sans-serif”>Price?:</font></td>
<td width=”180″><select name=”price” id=”price” onchange=”display(this,’0′,’1′);”><option value=”0″>No</option><option value=”1″>Yes</option></select></td>
</tr>
<tbody id=”1″ style=”display: none;”>
<tr>
<td width=”85″>Price Font:</td>
<td width=”180″><select name=”pricefont” id=”pricefont”><option value=”ffffff”>White</option><option value=”000000″>Black</option></select></td>
</tr>
</tbody>
<tbody id=”0″ style=”display: none;”>
<tr>
<td width=”85″></td>
<td width=”180″><input name=”pricefont” type=”hidden” id=”pricefont” value=””></td>
</tr>
</table>

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@Errant_ShadowOct 04.2010 — Well, what's happening is that your 2nd row is confining itself to the width of the 1st cell. It's acting like the tbody is a cell with a colspan of 1, though I don't know why.

Give your table a border of 1 and you'll see what I mean.
Copy linkTweet thisAlerts:
@Errant_ShadowOct 04.2010 — Also, I don't quite understand why you're turning on the display of a hidden form element.
Copy linkTweet thisAlerts:
@Errant_ShadowOct 04.2010 — Turns out you can't use display: none on a <tr> element, and I have no idea why the <tbody> element was being scrunched like it was.

It may not be the most elegant solution, but this is what I would do in a pinch:
[code=html]
<script type="text/javascript">
// <![CDATA[
function togglePriceFont(value)
{
document.getElementById('pricefontrow').style.display =(value == 0)? 'none' : 'block';
}
// ]]>
</script>

<table cellpadding="0" cellspacing="0">
<tr>
<td width="85">
<font size="2" face="Verdana, Arial, Helvetica, sans-serif">Price?</font>
</td>
<td width="180">
<select name="price" id="price" onchange="togglePriceFont(this.value);">
<option value="0">No</option>
<option value="1">Yes</option>
</select>
</td>
</tr>
<tr>
<td colspan="2">
<table id="pricefontrow" style="display: none;" width="100%" cellpadding="0" cellspacing="0">
<tr>
<td width="85">
<font size="2" face="Verdana, Arial, Helvetica, sans-serif">Price Font:</font>
</td>
<td width="180">
<select name="pricefont" id="pricefont">
<option value="ffffff">White</option>
<option value="000000">Black</option>
</select>
</td>
</tr>
</table>
</td>
</tr>
</table>
[/code]
Copy linkTweet thisAlerts:
@FangOct 04.2010 — tbody is not a [I]block [/I]element, use [I]table-row-group[/I]

http://www.w3.org/TR/CSS2/tables.html#value-def-table-row-group

[I]id[/I] is invalid: http://www.w3.org/TR/REC-html40/types.html#type-name

@Errant_Shadow: nested tables should be avoided
Copy linkTweet thisAlerts:
@Errant_ShadowOct 04.2010 — @Errant_Shadow: nested tables should be avoided[/QUOTE]

This is true.

So you're saying that the <tbody> element is acting like that because we're setting it to a display type that it's not compatible with? So... if he sets the display to "table-row-group" instead of "block" it should behave?
Copy linkTweet thisAlerts:
@FangOct 04.2010 — Yes, not all elements are [I]block [/I]elements, although older versions of IE only use the distinction [I]inline[/I] and [I]block[/I]
Copy linkTweet thisAlerts:
@Errant_ShadowOct 04.2010 — Looks like, yes, that does work. That'll make my future use of expanding/collapsing tags a lot easier; thank you Fang.

[code=html]
<script type="text/javascript">
// <![CDATA[
function togglePriceFont(value)
{
document.getElementById('pricefontrow').style.display =(value == 0)? 'none' : 'table-row-group';
}
// ]]>
</script>

<table>
<tr>
<td width="85"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Price?:</font></td>
<td width="180"><select name="price" id="price" onchange="togglePriceFont(this.value);"><option value="0">No</option><option value="1">Yes</option></select></td>
</tr>
<tbody id="pricefontrow" style="display: none;">
<tr>
<td width="85">Price Font:</td>
<td width="180"><select name="pricefont" id="pricefont"><option value="ffffff">White</option><option value="000000">Black</option></select></td>
</tr>
</tbody>
</table>
[/code]
Copy linkTweet thisAlerts:
@FangOct 04.2010 — A better way is to clear the property value (re-set), then there is no complication as to using the correct value for a particular element.[CODE]document.getElementById('pricefontrow').style.display = (value == 0)? 'none' : [COLOR="Blue"]''[/COLOR];[/CODE]
Copy linkTweet thisAlerts:
@cedric813authorOct 04.2010 — That works awesome!!! Thanks sooooo much to both of you for your help!!! :-)
×

Success!

Help @cedric813 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.18,
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,
)...