0

I tried to disable a table by using JavaScript if two input values are equal.

My code:

<script type="text/javascript" charset="utf-8">
    function checkEnableTable() {
        var totalacceptedload = document.getElementById('totalacceptedload'),
        var maximumload = document.getElementById('maximumload'),
        tablecoursedistribution = document.getElementById('tablecoursedistribution');
        tablecoursedistribution.disabled = (totalacceptedload.value = maximumload.value);
    }
    window.onload = checkEnableTable;
</script>

What am I doing wrong?

My Html

    <input id="totalacceptedload" class ="alert alert-success" value ="<?php if (!isset($count_course_choice)):?><?php else: echo $count_course_choice['contact_hours'];?><?php endif ?>"/>

<input id="maximumload" class ="alert alert-danger" value ="<?php if (!isset($eee_setting)): ?><?php else:echo $eee_setting['maximum_course_choice'];?><?php endif ?>"/>
<table id="tablecoursedistribution" >

</table>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mehedi Hasan
  • 61
  • 1
  • 10
  • You should use `totalacceptedload.value === maximumload.value` for boolean comparison. You are currently using assignment `=`. – Kavka Mar 27 '14 at 19:10
  • @Kavka I try what you suggested but still not working. are you able to show me another way to this .. pls – Mehedi Hasan Mar 27 '14 at 19:25
  • Apparently you cannot disable tables in the sense that you want: http://stackoverflow.com/questions/7899453/how-to-disable-and-enable-html-table-using-javascript The `` element does not even have a `disabled` property. Your code is just adding that property to the object.
    – Kavka Mar 27 '14 at 19:26
  • how can i able to hide table if two value is equal instead of disable. – Mehedi Hasan Mar 27 '14 at 19:48
  • 1
    `tablecoursedistribution.style.visibility = (totalacceptedload.value == maximumload.value) ? "hidden" : "visible"`. – Kavka Mar 27 '14 at 20:05

2 Answers2

1

this statement first check both values if equal then assign true to tablecoursedistribution.disabled otherwise false to tablecoursedistribution.disabled

tablecoursedistribution.disabled = (totalacceptedload.value == maximumload.value) ? "true" : "false" ; 
Anni
  • 142
  • 2
  • 16
0

You can do it like this:

function checkEnableTable() {
    tablecoursedistribution.style.visibility = (totalacceptedload.value == maximumload.value) ? "hidden" : "visible"
}

var totalacceptedload = document.getElementById('totalacceptedload'),
    maximumload = document.getElementById('maximumload'),
    tablecoursedistribution = document.getElementById('tablecoursedistribution');

window.onload = checkEnableTable;

Here's a fiddle.

I made it so that if you enter two equal values in the box, then click the button, the table is hidden, otherwise it is shown.

HTH

James Hibbard
  • 16,490
  • 14
  • 62
  • 74