0

When the option rond is selected in the select-list edit_type below I need input-field edit_maat_2 to be changed to readonly but nothing is happening.

This is my code:

<select class="form-control" id="edit_type['.$i.']" name="edit_type" onclick="show_dim(this, '.$i.')">
<option ';if($row_table_1['type'] == 'rond') { echo 'selected="selected"';} echo 'value="as">Staf rond</option>
<option ';if($row_table_1['type'] == 'buis') { echo 'selected="selected"';} echo 'value="buis">Buis</option>
</select>

<input type="number" class="form-control" id="edit_maat_2['.$i.']" name="edit_maat_2" value="'.$row_table_1['maat_2'].'">

<script type="text/javascript">
function show_dim(selectVeld, nr)
{
    if(document.getElementById('edit_type['+nr+']').value == 'rond') {
        document.getElementById('edit_maat_2['+nr+']').attr('readonly','readonly'); }
}
</script>

$i is generated and is function ok.

When I check this is console mode I see this error: Uncaught ReferenceError: e is not defined I am not using the variable e any wehere(?)

Any suggestions?

5
  • What is $i? Is the HTML generated with php? Commented Aug 30, 2018 at 8:31
  • $i is generated and is function ok. Commented Aug 30, 2018 at 8:32
  • Please click <> and create a minimal reproducible example with PURE HTML and JS only. No need for PHP Commented Aug 30, 2018 at 8:38
  • 1
    or document.getElementById('edit_maat_2['+nr+']').readOnly = true; Commented Aug 30, 2018 at 8:42
  • 1
    Try to watch this,maybe it will help you jsfiddle.net/Micio/32uqjh84/4 Commented Aug 30, 2018 at 8:48

1 Answer 1

2

Several issue, many typos.

  • You have values "as" and "buis" - nowhere "rond"
  • .attr is jQuery, not DOM

This is likely what you want

function setAccess() {
  var id = this.id.replace("edit_type", "edit_maat_2"),
    field = document.getElementById(id);
  if (this.value == "as") {
    field.setAttribute('readonly', 'readonly');
  } else {
    field.removeAttribute('readonly');
  }

}

window.addEventListener("load", function() {
  document.querySelectorAll("[id^=edit_type]").forEach(function(sel) {
    sel.addEventListener("change", setAccess);
    // Initialise the fields in case of PHP setting the selected attribute
    if ("createEvent" in document) {
      var evt = document.createEvent("HTMLEvents");
      evt.initEvent("change", false, true);
      sel.dispatchEvent(evt);
    } else {
      sel.fireEvent("onchange");
    }
  });
})
<select class="form-control" id="edit_type[1]" name="edit_type">
  <option value="as">Staf rond</option>
  <option value="buis">Buis</option>
</select>

1 <input type="number" class="form-control" id="edit_maat_2[1]" name="edit_maat_2" value="1">

<select class="form-control" id="edit_type[2]" name="edit_type">
  <option value="as">Staf rond</option>
  <option value="buis">Buis</option>
</select>

2 <input type="number" class="form-control" id="edit_maat_2[2]" name="edit_maat_2" value="2">

Sign up to request clarification or add additional context in comments.

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.