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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript function to enable textfield on dropdown select

Status
Not open for further replies.

scubab321

Technical User
Dec 4, 2009
3
0
0
US
This script works to enable the textfield 'otherz' from
'D1' select option 'Others'.

HTML
<select size="1" name="D1" onChange="dis_able()">
<option>Category A</option>
<option>Category B</option>
<option>Category C</option>
<option value="Others">Others</option>
</select>
<input disabled type="text" name="otherz" size="20" value="pls specify"></p>

Javascript
function dis_able()
{
if(document.myform.D1.value != 'Others')
document.myform.otherz.disabled=1;
else
document.myform.otherz.disabled=0;
}

My questions is: how can this be rewritten so it will enable/disable other textfields associated with other dropdown menus? (as if we have the same code, but the select name "D2", "D3"...etc. and text field to enable associated with that select: "otherz2", "otherz3".

Thank you for your help.
 
Hi

Use parameters ?
HTML:
[b]<select[/b] [maroon]size[/maroon][teal]=[/teal][green][i]"1"[/i][/green] [maroon]name[/maroon][teal]=[/teal][green][i]"D1"[/i][/green] [maroon]onChange[/maroon][teal]=[/teal][green][i]"dis_able([highlight]this,'Others','otherz'[/highlight])"[/i][/green][b]>[/b]
  [b]<option>[/b]Category A[b]</option>[/b]
  [b]<option>[/b]Category B[b]</option>[/b]
  [b]<option>[/b]Category C[b]</option>[/b]
  [b]<option[/b] [maroon]value[/maroon][teal]=[/teal][green][i]"Others"[/i][/green][b]>[/b]Others[b]</option>[/b]
[b]</select>[/b]
[b]<input[/b] [maroon]disabled[/maroon] [maroon]type[/maroon][teal]=[/teal][green][i]"text"[/i][/green] [maroon]name[/maroon][teal]=[/teal][green][i]"otherz"[/i][/green] [maroon]size[/maroon][teal]=[/teal][green][i]"20"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"pls specify"[/i][/green][b]></p>[/b]
JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]dis_able[/color][teal]([/teal][highlight]what[teal],[/teal]morevalue[teal],[/teal]moreinput[/highlight][teal])[/teal]
[teal]{[/teal]
  [highlight]what[teal].[/teal]form[teal].[/teal]elements[teal][[/teal]moreinput[teal]].[/teal]disabled[teal]=[/teal]what[teal].[/teal]value[teal]!=[/teal]morevalue[/highlight]
[teal]}[/teal]

Feherke.
 
Thank you for your fast reply. However, what I had in mind (and forget to put down in thread) was a function written with DOM.

Find all selects in the form, for all selects if value 'Others' is selected than whatever next text field is get enabled.

I mean, can this be rewritten with DOM in mind?
 
Hi

Possible. However I am not adept of such practices.
Code:
[b]<select[/b] [maroon]size[/maroon][teal]=[/teal][green][i]"1"[/i][/green] [maroon]name[/maroon][teal]=[/teal][green][i]"D1"[/i][/green] [maroon]onChange[/maroon][teal]=[/teal][green][i]"dis_able(this)"[/i][/green][b]>[/b]
JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]dis_able[/color][teal]([/teal]what[teal])[/teal]
[teal]{[/teal]
  [b]var[/b] next[teal]=[/teal]what[teal].[/teal]nextSibling
  [b]while[/b] [teal]([/teal]next [teal]&&[/teal] next[teal].[/teal]nodeName[teal].[/teal][COLOR=darkgoldenrod]toLowerCase[/color][teal]()!=[/teal][green][i]'input'[/i][/green][teal])[/teal] next[teal]=[/teal]next[teal].[/teal]nextSibling
  [b]if[/b] [teal]([/teal]next[teal])[/teal] next[teal].[/teal]disabled[teal]=[/teal]what[teal].[/teal]value[teal]!=[/teal][green][i]'Others'[/i][/green]
[teal]}[/teal]
The above works as long as there is no block element between the [tt]select[/tt] and the [tt]input[/tt].

Feherke.
 
superb. works like a charm. thanks a lot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top