2

I have 1 row and 3 column. Each column have 3 buttons with different ids.

example id:

--column 1--       --column 2--       --column 3--
id="1morning"      id="2morning"      id="3morning"
id="1evening"      id="2evening"      id="3evening"
id="1afternoon"    id="2afternoon"    id="3afternoon"

*I want to disable button in column 2 with id="2evening"

I know how to disable button using javascript but I do not know how to manipulate the javascript so that I can disable the button that I want.

HTML code:

<br>
<div class="col-md-9">
  <div class="well.hilang"> 
    <table>
      <tr>
       <?php
       for ($i=1; $i <=3 ; $i++) { 
         ?>
         <td>
           <?php echo $i;?>  
           <div class="form-group">
             <input class="btn btn-l" type="submit" name="submit" id="<?php echo $i;?>morning" ><br>
             <input class="btn btn-l" type="submit" name="submit" id="<?php echo $i;?>evening" ><br>
             <input class="btn btn-l" type="submit" name="submit" id="<?php echo $i;?>afternoon" >
           </div>
         </td>
         <?php }
         ?>
       </tr>
     </table>
   </div>
 </div>

JS code:

<?php for ($i=1; $i <=3 ; $i++) { ?>
<script>
  document.getElementById("<?php echo $i;?>evening").disabled = true;
</script>
<?php } ?>

The problem is the Javascript disabled button in all column that have id="1evening", id="2evening" and id="3evening". What I want is to disable button with id="2evening" only.

Thanks, faizal

blackpla9ue
  • 3,191
  • 20
  • 29
Fai Zal Dong
  • 315
  • 3
  • 21

2 Answers2

3

Your selector isn't correct. In document.getElementById(<id here>), you just need to tell it the element ID that you want to work with. So if you want to disable the 2evening button, you would do the following:

document.getElementById("2evening").disabled = true;

More Information: How to disable html button using JavaScript?

Community
  • 1
  • 1
mwilson
  • 10,186
  • 5
  • 41
  • 72
0

Make function for than then you can use with every event like onload or onclick

HTML

<body onload="myFunction()" >
    <button id="myBtn" >My Button</button>
    <!--<button id="makesomething" onclick="myFunction()">Click Here to Disable above button</button>-->
</body>

JavaScript

<script>
function myFunction() {
    document.getElementById("myBtn").disabled = true;
}
</script>
Gaurang Joshi
  • 629
  • 15
  • 30