2

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?

5
  • 1
    how nice if you can remove php and provide a fiddle 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. 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. 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 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. Commented Apr 16, 2016 at 7:22

6 Answers 6

1

You could use just one function:

function toggle(someId) {
    document.getElementById(someId).disabled = !document.getElementById(someId).disabled;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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; ?>"/> 

Comments

0

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;
}

1 Comment

He used the checked boolean of the checkbox correctly
0

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; }

Comments

0

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.net/ravipateldhg/5c7rdt0a

Comments

0

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 recommended.

For reference you can check https://jsfiddle.net/uw5f001q/3/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.