Current location: Hot Scripts Forums » General Web Coding » JavaScript » Update textbox style with javascript


Update textbox style with javascript

Reply
  #1 (permalink)  
Old 09-30-06, 07:17 AM
HairySpider's Avatar
HairySpider HairySpider is offline
Newbie Coder
 
Join Date: Jan 2005
Location: Herts, UK
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
Update textbox style with javascript

Hi all, I hope someone can help me

I need the style attribute of a textbox to change to
Code:
style = "Champ"
if the box is checked and
Code:
style = ""
if the box is not checked (i.e. unchecked after being checked). I am stuck on this. So far I have tried:
Code:
    <td rowspan="16" align="center" valign="middle" onclick="setfocusparents1()"><input type="text" name="parents1" class="" /><br />
      <input name="parents1champ" type="checkbox" id="parents1champ" onselect="checkparents1()" onchange="checkparents1()">Champion</input> </td>
with
Code:
<script type="text/javascript">
function setfocusparents1()
{
document.forms[0].parents1.focus()
}

function checkparents1()
{
document.forms[0].parents1.class = "Champ"
}
</script>
Those are the relivent parts of the page.

Any help greatly appreciated

Cheers, Luke
Reply With Quote
  #2 (permalink)  
Old 09-30-06, 07:39 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Try this.
Code:
<script type="text/javascript">
function setfocusparents1()
{
	document.forms[0].parents1.focus()
}

function checkparents1(val)
{
	var form = document.forms[0];
	var classname = val ? 'Champ' : '';

	form.parents1.setAttribute('class', classname);
	form.parents1.setAttribute('className', classname);
}
</script>

  <td rowspan="16" align="center" valign="middle" onclick="setfocusparents1()">
  		<input type="text" name="parents1" /><br />
      <input name="parents1champ" type="checkbox" id="parents1champ" onclick="checkparents1(this.checked)">Champion</input> 
	  </td>
Reply With Quote
  #3 (permalink)  
Old 09-30-06, 07:42 AM
HairySpider's Avatar
HairySpider HairySpider is offline
Newbie Coder
 
Join Date: Jan 2005
Location: Herts, UK
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
Cheers mate It works perfectly!
Reply With Quote
  #4 (permalink)  
Old 09-30-06, 08:50 AM
HairySpider's Avatar
HairySpider HairySpider is offline
Newbie Coder
 
Join Date: Jan 2005
Location: Herts, UK
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks, but I will be having about 100 input boxes on this page and I want each one to do this. I could write out the function 100 times, but I could I do something like this;
Code:
function checkparents1(val,field)
{
	var form = document.forms[0].field;
	var classname = val ? 'Champ' : '';

	form.parents1.setAttribute('class', classname);
	form.parents1.setAttribute('className', classname);
}
and then call it like this:
Code:
<input name="parents1champ" type="checkbox" id="parents1champ" onclick="checkparents1(this.checked,parents1)">Champion</input>
I cant seem to get that to work

Luke

Last edited by HairySpider; 09-30-06 at 08:59 AM.
Reply With Quote
  #5 (permalink)  
Old 10-01-06, 04:08 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
That won't work.
If you want to use a variable in a reference, you need to use [variable] instead of .variable or the browser will think you mean the property/object named "variable".
You also need to have the variable value as a string or index number.

Code:
function checkParents(checked,fieldName){
 var theField=document.forms[0][fieldName]
 if(!theField){
   return false; // No such field existed
 }
 var classname = checked = "Champ" : "";
  theField.setAttribute('class', classname);
  theField.setAttribute('className', classname);
}

<input name="parents1champ" type="checkbox" id="parents1champ" onclick="checkparents1(this.checked,'parents1')">Champion</input>
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
  #6 (permalink)  
Old 10-01-06, 05:08 PM
HairySpider's Avatar
HairySpider HairySpider is offline
Newbie Coder
 
Join Date: Jan 2005
Location: Herts, UK
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks TwoD
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript tinto form textbox jac2703 JavaScript 5 08-31-06 07:53 AM
Update: I can't update multi data into DB ahlai Database 4 08-10-06 12:55 PM
Need help editing a .class CSS style with javascript nova912 JavaScript 2 05-26-06 04:51 PM
Update with javascript? chrille112 JavaScript 1 11-23-05 05:02 PM
Reaaly stuck about javascript over frames muratisik JavaScript 1 12-14-03 11:58 AM


All times are GMT -5. The time now is 11:20 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.