0

I have a problem on this, i want to enabled the select element by checking the check box that correspond the select element in a row. how can i do that?

<table>
<?php do { ?>
 <tr>
 <td><input type="checkbox" /></td>
 <td>
    <select disabled="disabled">
     <option value="1">Dog</option>
     <option value="2">Cat</option>
    </select>
 </td>
</tr>
<?php } while($row = mysql_fetch_assoc($result)); ?>
</table>

4 Answers 4

2

You can try this is Javascript like create a function like

function CheckMe(field)
{
    var tr = field.parentNode.parentNode;
    var select = tr.getElementsByTagName("select");

    if(select.length>0)
    {
        if(field.checked)
        {
            select[0].removeAttribute("disable");
        }
        else
        {
            select[0].setAttribute("disable", "disable")  
        }
    }
}

and change in you php do loop

<td><input type="checkbox" /></td>

to

<td><input type="checkbox" onclick="CheckMe(this)" /></td>
Sign up to request clarification or add additional context in comments.

1 Comment

Good, clean answer though I prefer not to use event attributes.
1

In plain javascript, this code will enable the select list in the same row when the checkbox is checked and disable it when it is unchecked:

var table = document.getElementById("myTable");
var checkboxes = table.getElementsByTagName("input");
for (var i = 0; i < checkboxes.length; i++) {
    checkboxes[i].addEventListener("click", function() {
        var row = this.parentNode.parentNode;
        var select = row.getElementsByTagName("select")[0];
        if (this.checked) {
           select.removeAttribute("disabled");
        } else {
           select.setAttribute("disabled", "disabled");
        }
    }, false);
}​

Working demo: http://jsfiddle.net/jfriend00/w8ZUj/

FYI - this uses addEventListener which is not supported in older versions of IE. A suitable cross browser replacement that substitutes addEvent in IE can be used if older versions of IE must be supported.

2 Comments

The way to go imo, though row.getElementsByTagName("select")[0] could be potentially undefined.
@Christoph - that depends upon the OP's HTML. It would only be undefined if there's no matching select element for a checkbox.
0

Others have given you answers based on parentNode, however a more general solution can be used with a simlpe upTo function, e.g.

function upTo(el, tagName) {
  tagName = tagName.toLowerCase();
  var el;
  do {
    el = el.parentNode;
    if (el.tagName.toLowerCase() == tagName) {
      return el;
    }
  } while (el.parentNode)
}

So to get the select element, the listener would be something like:

<input type="checkbox" onclick="doSelect(this);">

where doSelect is:

function doSelect(el) {
  var sel;
  var row = upTo(el, 'tr');

  if (row) {
    sel = row.getElementsByTagName('select')[0];
  }

  if (sel) {
    sel.disabled = !el.checked;
  }
}

Comments

-1

You can add onchange event to the checkbox as follows:

<input type="checkbox" onchange="enableSelect(this)" />

and then use this javascript code to enable the selectbox in the same row as the checkbox

function enableSelect(checkbox) {
    var selectElements = checkbox.parentNode.parentNode.getElementsByTagName("select");
    selectElements[0].disabled = !checkbox.checked;
}

9 Comments

No, but it would have been easier if @avien used jQuery to accomplish this. Can't you read the first line? It is just a suggestion.
@Christoph - there is a javascript tag, so I don't see why a Javascript solution wouldn't be appropriate.
thanks for the response guys.. anyway i forgot to ask, is there anyway to solve this problem without using jquery? just pure javascript will do.
@Spudley javascript != jQuery
i mean just the pure and traditional approach of using javascript ;)
|

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.