1
echo ' 
<td><input type="checkbox" name="'.$data2["name"].'" value="'.$data2["name"].'" 
onclick="document.getElementById(numberof'.$data2["name"].').disabled=this.checked;"></td>

<td><input type="text" size="1" name="numberof'.$data2["name"].'"  value="1" " ></td>';

Hello!

I used the last solution in this question but it don't work.

How to disable textbox depending on checkbox checked

What can be wrong with it?

Best regards, Susi :*

Community
  • 1
  • 1
Susanne92
  • 217
  • 1
  • 6
  • 21

5 Answers5

1

Try to replace

onclick="document.getElementById(numberof'.$data2["name"].')

with

onclick="document.getElementsByName(numberof'.$data2["name"].')

Because there is no id for your textbox,you are using name only.So get the element by name.

You can try like this also(optionaly)

<td><input type="checkbox" name="<?=$data2[\"name\"];?>" value="<?=$data2[\"name\"];?>" 
    onclick="document.getElementsByName('numberof<?=$data2[\"name\"];?>"').disabled=this.checked;">
</td>
<td>
    <input type="text" size="1" name="numberof<?=$data2[\"name\"];?>"  value="1" " >
</td>
GautamD31
  • 28,552
  • 10
  • 64
  • 85
1

You are using getElementById, but your textfield only uses a name and no id.

This is a quick fix:

<td><input type="text" size="1" id = "numberof'.$data2["name"].'" name="numberof'.$data2["name"].'"  value="1" " ></td>';
Skarlinski
  • 2,419
  • 11
  • 28
1
echo " 
 <td><input type='checkbox' name='".$data2['name']."' value='".$data2['name']."'
 onclick=\"document.getElementById('numberof".$data2['name']."').disabled=this.checked;\">    </td>

 <td><input type='text' size='1' id='numberof".$data2['name']."'  value='1'></td>";
Srikanth Kolli
  • 892
  • 1
  • 8
  • 19
1

Since there is no id but only a name for the text input, you need to access it using the javascript function getElementsByName().

onclick="document.getElementsByName(numberof'.$data2["name"].').disabled=this.checked;"></td>

Another mistake you have done is forgetting to enclose the name within quotes.

getElementById(\'numberof'.$data2["name"].'\')

So, your final code should look like this:

echo ' 
<td><input type="checkbox" name="'.$data2["name"].'" value="'.$data2["name"].'" 
onclick="document.getElementsByName(\'numberof'.$data2["name"].'\').disabled=this.checked;"></td>

<td><input type="text" size="1" name="numberof'.$data2["name"].'"  value="1" " ></td>';
Kevin
  • 6,539
  • 5
  • 44
  • 54
1

Use the below code:

echo ' 
<td><input type="checkbox" name="'.$data2["name"].'" value="'.$data2["name"].'" 
onclick="document.getElementsByName(\'numberof'.$data2["name"].'\')[0].disabled=this.checked;"></td>

<td><input type="text" size="1" name="numberof'.$data2["name"].'"  value="1" " ></td>';
Code Lღver
  • 15,573
  • 16
  • 56
  • 75