Connecting Tech Pros Worldwide Forums | Help | Site Map

enable a text field onclick or onchange

Member
 
Join Date: Oct 2006
Posts: 102
#1: Nov 13 '08
I dont know if im postin in the right forum coz im not sure whether this is JS or PHP problem ..
my problem is i have a text field which is disabled and i need to enable it when the user selects specific option so this should be done through the id of the field but this field is generated in a while loop so only the first text field gets affected by my javascript function ..i know the id should be unique so how shall i implement this :
here's the code:
Expand|Select|Wrap|Line Numbers
  1. function enable()
  2.     {   
  3.        document.getElementById("demo1").disabled=false;  
  4.     }
  5.  while($row=mysql_fetch_array($query))
  6.                    {
  7.                                 echo" <td> <select name=status onchange=enable()>";?>
  8.                                 <option <?if($row[order_status]=='pending') echo "selected"?> >pending</option>
  9.                                  <option <?if($row[order_status]=='shipped') echo "selected"?>>shipped</option>
  10.                                  </td>
  11. <td><input type="text" id="demo1" name="date" value="<?echo $arr[0]?>" disabled="disabled"

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,660
#2: Nov 13 '08

re: enable a text field onclick or onchange


add a number to the id name (e.g. by a count variable).
[PHP]$count = 0;
while ([…]) {
// code
echo "[…] <input id=\”demo$count\" […]";
$count++;
}[/PHP]
this works for a known amount (1) of fields.

if you want to activate more than one (or an unknown amount) set a class attribute and check that in your javascript function (be aware that the implementation of getElementsByClassName() is different between browsers, if at all).

to write valid HTML code NEVER use duplicate ids (as you see it will sooner or later mess up something)

regards
Member
 
Join Date: Oct 2006
Posts: 102
#3: Nov 13 '08

re: enable a text field onclick or onchange


what u said sounds Great but how can i change that JS line :

document.getElementById("demo<?=$count?>").disable d=false;
its not working !!
Member
 
Join Date: Oct 2006
Posts: 102
#4: Nov 13 '08

re: enable a text field onclick or onchange


U know i think i figured it out :)

but Thanks a Million for your advice and ur great Help



i love you Bytes
Reply