Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change background color with function 1

Status
Not open for further replies.

tabbytab

Technical User
Mar 21, 2005
74
GB
Hi guys

I am totally new to javascript so excuse the crumby code....

I am trying to change the background color of an input box when a radio button is clicked.

I am successfully changing the text in the input box when the radio button is clicked but have no idea as to syntax to change color.

The input box controlname and text value are passed to the function which allows me to reuse the function.

Code:
function J(QName,SelectedValue)
{
if (QName.substr(1,1) =="A")
eval('document.forms[0].RA'+(QName.substr(2,(QName.length)-2))+'.value=SelectedValue');

if (QName.substr(1,1) =="B")
eval('document.forms[0].RB'+(QName.substr(2,(QName.length)-2))+'.value=SelectedValue');
}

I've tried

Code:
function J(QName,SelectedValue)
{
if (QName.substr(1,1) =="A")
eval('document.forms[0].RA'+(QName.substr(2,(QName.length)-2))+'.value=SelectedValue');

eval('document.forms[0].RA'+(QName.substr(2,(QName.length)-2))+'.style.backgroundcolor="#000000"');


if (QName.substr(1,1) =="B")
eval('document.forms[0].RB'+(QName.substr(2,(QName.length)-2))+'.value=SelectedValue');
}

Any pointers

Many thanks in advance
TabbyTab

 
I'd go with this:

Code:
function J(QName,SelectedValue) {
	var frm = document.forms[0].elements;
	if (QName.substr(1,1) == 'A') {
		frm['RA' + QName.substr(2, QName.length - 2)].value = SelectedValue;
		frm['RA' + QName.substr(2, QName.length - 2)].style.backgroundColor = '#000000';
	}

	if (QName.substr(1,1) == 'A') {
		frm['RB' + QName.substr(2, QName.length - 2)].value = SelectedValue;
		frm['RB' + QName.substr(2, QName.length - 2)].style.backgroundColor = '#000000';
	}
}

When has the added benefit of removing the use of eval (which is always best avoided).

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Ok - minor typo in that second bit (== 'A' should be == 'B')... but if the radio you are selecting is always asscoiated with the letter, you could actually get away with even less code:

Code:
function J(QName, SelectedValue) {
	var frm = document.forms[0].elements;
	var letter = QName.substr(1, 1);
	
	frm['R' + letter + QName.substr(2, QName.length - 2)].value = SelectedValue;
	frm['R' + letter + QName.substr(2, QName.length - 2)].style.backgroundColor = '#000000';
}

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Or even:

Code:
function J(QName, SelectedValue) {
	var radio = document.forms[0].elements['R' + QName.substr(1, QName.length - 2)];
	radio.value = SelectedValue;
	radio.style.backgroundColor = '#000000';
}

and if QName begins with "R", you could use:

Code:
function J(QName, SelectedValue) {
	var radio = document.forms[0].elements[QName.substr(0, QName.length - 2)];
	radio.value = SelectedValue;
	radio.style.backgroundColor = '#000000';
}

I'm sure you get the idea by now ;-)

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Cool - many thanks for that - it works a treat.

I guess I have a lot to learn! :)

TabbyTab:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top