tenten
tenten

Reputation: 1276

Disabling text field by Javascript

I have a inputing HTML page that have text field and a check box for disabling that input field.

This is the JavaScript code for disabling the text field in a .js file.value is passing the field name for check box.

   function disable_input_field(value){     
        if ($("#undefined_" + value).is(":checked")) {       
            document.getElementById("undefined_" + value).disabled = true;       
            document.getElementById(value).value = '??';     
        }else{      
            $("#" + value ).prop("disabled", false);        
            document.getElementById(value).value = '';     
        } 
    }

In the imputing HTML page this is the html code and the js code.

<tr>
                <td >data</td>
                    <td colspan="3">
                       <input type="text" id="data_value" name="data_vale" size="50"value='<?php echo $data?>'>
                </td>
                <td colspan = "3">
                <?php 
                    if($check_data != ''){?>
                        <input type="checkbox" checked name="undefined_data" id="undefined_data" onclick="disable_input_field('data')" >disable
                <?php }else{?>
                        <input type="checkbox" name="undefined_data" id="undefined_data" onclick="disable_input_field('data')" >disable
                <?php }?>
                <input type="hidden" name="data" id="data">
                </td>
            </tr>
            <tr>

................................................................................

$(document).ready(function() {
    if(document.getElementById('check_data').value == 'data'){
       disable_input_field('data');
    }
 }      

In the controller,

if ($this->input->post('data') == 'disabled') {
    $data['check_data'] = 'data';
}

But disabling the text field not working. Please help me on this.

Upvotes: 1

Views: 2294

Answers (4)

mrid
mrid

Reputation: 5796

You are referencing to check_data in your jquery

$(document).ready(function() {
    if(document.getElementById('check_data').value == 'data'){
       disable_input_field('data');
    }
}

But you haven't made the element yet.

Also I use this to disable controls, so thought might help :)

$(document).ready(function(){

$(document).on('change', '#check1', function(e){
  if($(this).is(':checked')){
      $('#input1').addClass('disabled-control');
  }
  else
  {
      $('#input1').removeClass('disabled-control');
  }
});


});
.disabled-control{
      opacity: 0.4; 
      cursor: not-allowed;
      pointer-events: none;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<input type="text" id="input1">
<input type="checkbox" id="check1">Disable

Upvotes: 1

Shailesh Kopardekar
Shailesh Kopardekar

Reputation: 91

You have used the ID "check_data" in below line, I don't see the "check_data" element in the specefied HTML. This might be the root cause, other code seems ok.

if(document.getElementById('check_data').value == 'data'){

Upvotes: 2

Adolphus Ald
Adolphus Ald

Reputation: 21

you may set disabled attribute for the element, for example

document.getElementById('data_value').setAttribute('disabled ', 'disabled');

Upvotes: 2

bmceldowney
bmceldowney

Reputation: 2305

It looks like you're disabling the checkbox instead of the textbox. Change the code from:

function disable_input_field(value){     
    if ($("#undefined_" + value).is(":checked")) {       
        document.getElementById("undefined_" + value).disabled = true;       
        document.getElementById(value).value = '??';     
    }else{      
        $("#" + value ).prop("disabled", false);        
        document.getElementById(value).value = '';     
    } 

to:

function disable_input_field(value){     
    if ($("#undefined_" + value).is(":checked")) {       
        document.getElementById("#" + value + "_value").disabled = true;       
        document.getElementById(value).value = '??';     
    }else{      
        $("#" + value + "_value" ).prop("disabled", false);        
        document.getElementById(value).value = '';     
    } 

Upvotes: 2

Related Questions