This is my checkbox and text box:
<td> <input type ="checkbox" id="haha" onclick="enable('<?php echo $agenda->id; ?>')" value=<?php echo $agenda->id; ?>>
<td> <input type="text" name="dnama" id="nama_<?php echo $agenda->id; ?>" value="<?php echo $agenda->nama; ?>" disabled /> </td>
<td> <input type="text" name="dketer" id="ket_<?php echo $agenda->id; ?>" value="<?php echo $agenda->keterangan; ?>" disabled> </td>
and here's my javascript:
function enable(id) {
var disabled = !document.getElementById('haha').checked;
document.getElementById("nama_"+id).disabled = disabled;
document.getElementById("ket_"+id).disabled = disabled; }
my question is why this is only works on first row, i checked first checkbox the textbox will be enabled and then i unchecked first checkbox and checked 2nd and others that textbox still disabled.. how can i fix this?
This is my checkbox and text box:
<td> <input type ="checkbox" id="haha" onclick="enable('<?php echo $agenda->id; ?>')" value=<?php echo $agenda->id; ?>>
<td> <input type="text" name="dnama" id="nama_<?php echo $agenda->id; ?>" value="<?php echo $agenda->nama; ?>" disabled /> </td>
<td> <input type="text" name="dketer" id="ket_<?php echo $agenda->id; ?>" value="<?php echo $agenda->keterangan; ?>" disabled> </td>
and here's my javascript:
function enable(id) {
var disabled = !document.getElementById('haha').checked;
document.getElementById("nama_"+id).disabled = disabled;
document.getElementById("ket_"+id).disabled = disabled; }
my question is why this is only works on first row, i checked first checkbox the textbox will be enabled and then i unchecked first checkbox and checked 2nd and others that textbox still disabled.. how can i fix this?
Share Improve this question asked Apr 16, 2016 at 6:58 Bobby PratamaBobby Pratama 961 gold badge2 silver badges13 bronze badges 5- 1 how nice if you can remove php and provide a fiddle – Madhawa Priyashantha Commented Apr 16, 2016 at 6:59
-
"why this is only workds on first row",
id
's are unique if you more than checbox with the same id#haha
it will work for the first one. – elreeda Commented Apr 16, 2016 at 7:05 - yeah thanks. you're right my checkbox has the same id #haha but different values. how can i fix that? i tried getElementByid('haha').value, but it can't work. – Bobby Pratama Commented Apr 16, 2016 at 7:13
- Please edit your question into a snippet (click the [<>] button) and remove all PHP since it is irrelevant to the question and actually looks like you need to fix the ID handling – mplungjan Commented Apr 16, 2016 at 7:13
- If it has same id rather than ID, pass this object and get the value and then get id and pass to dependent elements for it id. – Suresh Commented Apr 16, 2016 at 7:22
6 Answers
Reset to default 1You could use just one function:
function toggle(someId) {
document.getElementById(someId).disabled = !document.getElementById(someId).disabled;
}
You want the following:
function testEna() {
var dis = this.checked;
document.getElementById("nama"+this.id).disabled=dis;
document.getElementById("ket"+this.id).disabled=dis;
}
window.onload=function() {
var ena = document.querySelectorAll(".enabler");
for (var i=0;i<ena.length;i++) {
ena[i].onclick=testEna;
ena[i].onclick(); // run now too
}
}
using
<input type="checkbox" class="enabler" id="<?php echo $agenda->id; ?>" />
<input type="text" name="dnama" id="nama_<?php echo $agenda->id; ?>" value="<?php echo $agenda->nama; ?>"/>
<input type="text" name="dketer" id="ket_<?php echo $agenda->id; ?>" value="<?php echo $agenda->keterangan; ?>"/>
I don't know your ID's but the disable and enable checkbox code is like this :
function enable() {
document.getElementById("some_id").disabled= false;
}
function disable() {
document.getElementById("some_id").disabled= true;
}
try this..
function EnableDisable(id) {
document.getElementById("nama_"+id).disabled =false;
document.getElementById("ket_"+id).disabled = false;
var disabled = !document.getElementById('haha').checked;
document.getElementById("nama_"+id).disabled = disabled;
document.getElementById("ket_"+id).disabled = disabled; }
I don't know you question exactly, But it's good to use jQuery for this. I have try something with this. See below jsfiddel link
jsfiddle/ravipateldhg/5c7rdt0a
As you said you are using same id twice .With out changing id u can use this. But u should update the parament i.e Rather than passing id you should pass this object
function enable(pobj) {
var id = pobj.value;
var disabled = !pobj.checked;
document.getElementById("nama_"+id).disabled = disabled;
document.getElementById("ket_"+id).disabled = disabled;
}
Preferably usage of same id is not remended.
For reference you can check https://jsfiddle/uw5f001q/3/