Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-27-2009, 03:58 PM   PM User | #1
elso
New to the CF scene

 
Join Date: May 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
elso is an unknown quantity at this point
Simple JS trouble with text manipulation

Hello

I don't understand why I am having this trouble. I have tried many variations, but to no avail.
The objective here is to change some displayed text based on radio button selection. My function script currently reads:
Code:
<script language="JavaScript" type="text/javascript">
function a10892F() {
	var MugSelect = document.getElementById('MugSelect').value;
	var choice1 = "glass";
	var choice2 = "deco";
			
	if (MugSelect == choice1)
		{
			document.getElementById('a10892').innerHTML = 'Text to display if glass is chosen';
		}
	else if (MugSelect == choice2)
		{
			document.getElementById('a10892').innerHTML = 'text for display if deco was the choice';
		}
	else
		{
			document.getElementById('a10892').innerHTML = 'Default text for any other choice';
		}
	
	}		
</script>
The radio buttons:
Code:
<input name="MugSelect" type="radio" value="deco" id="MugSelect" onclick='a10892F()' />
<input name="MugSelect" type="radio" value="18oz" id="MugSelect" onclick='a10892F()' />
<input name="MugSelect" type="radio" value="glass" id="MugSelect" onclick='a10892F()' />
<input name="MugSelect" type="radio" value="15oz" id="MugSelect" onclick='a10892F()' />
<input name="MugSelect" type="radio" value="22oz" id="MugSelect" onclick='a10892F()' />
Changing text section:
Code:
<div align="center" id="a10892">This is the text I want to change based on the radio selection</div>
I get no errors, and the script is being read, but it seems the conditions are being ignored. If I select "deco" in the radio buttons, my expectation is the script would halt as soon as the "else if (MugSelect == choice2)" is seen as true. But it doesn't, it executes through to the final "else" regardless, and displays that message.

Can some one please show me what I am doing wrong here?

Many thanks
Elso
elso is offline   Reply With Quote
Old 06-27-2009, 05:30 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Try this:-

Code:
<script type="text/javascript">
function a10892F() {

for (var i =0;i<document.myform.MugSelect.length; i++) {
if (document.myform.MugSelect[i].checked) {
var MugSel = document.myform.MugSelect[i].value
}
}

	var choice1 = "glass";
	var choice2 = "deco";
			
	if (MugSel == choice1)
		{
			document.getElementById('a10892').innerHTML = 'Text to display if glass is chosen';
		}
	else if (MugSel == choice2)
		{
			document.getElementById('a10892').innerHTML = 'text for display if deco was the choice';
		}
	else
		{
			document.getElementById('a10892').innerHTML = 'Default text for any other choice';
		}
	
	}		
</script>

<form name = "myform">
<input name="MugSelect" type="radio" value="deco" id="MugSelect" onclick='a10892F()' />
<input name="MugSelect" type="radio" value="18oz" id="MugSelect" onclick='a10892F()' />
<input name="MugSelect" type="radio" value="glass" id="MugSelect" onclick='a10892F()' />
<input name="MugSelect" type="radio" value="15oz" id="MugSelect" onclick='a10892F()' />
<input name="MugSelect" type="radio" value="22oz" id="MugSelect" onclick='a10892F()' />
</form>

<div align="center" id="a10892">This is the text I want to change based on the radio selection</div>

Comments:-

You can only establish the value of a radio button group by polling.

var MugSelect = document.getElementById('MugSelect').value;
Never use the same name for a Javascript variable as that of an HTML element.

<SCRIPT language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.



"A man would do nothing, if he waited until he could do it so well that no one at all would find fault with what he has done." - Cardinal Newman
Philip M is offline   Reply With Quote
Old 06-27-2009, 07:00 PM   PM User | #3
elso
New to the CF scene

 
Join Date: May 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
elso is an unknown quantity at this point
Smile

PERFECT!!

Thank you very much
It has been many years since I last worked with JS.

Thanks again
elso is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:33 AM.


Advertisement
Log in to turn off these ads.