We help IT Professionals succeed at work.

disable textbox

jagguy
jagguy asked
on
Hi,
 
In cakephp I want to be able select a date only if I chose 'custom date' from drop down box.
If custom date is not selected I want the texboxes not able to receive input.

I think Ajax is needed but I can do it.

Currently the date can be selected anytime .

    $dateRange=array(0 => 'Fortnightly',1 => 'Today',2=> 'Month', 3 => 'Custom Date');
       

 echo $this->Form->input('dateRange', array('label' => 'Date Range',
                      'options' => $dateRange,'style'=>'width:150px','selected' => $dateSelect));
     

  //select date
    echo $this->Form->input('startDate',array('label' => 'Start Date','class'=>'datepicker', 'type'=>'text','style'=>'width:100px;height:30px','value' => $datestart));
Comment
Watch Question

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Hey jagguy,

You don't need to use AJAX, but you will need to use javascript. Probably just as easy to do it with jQuery (I already know you're using it).

Add this to your the <script> tag in the <head> or your document:

$(document).ready(function() {
    $('select[name=dateRange]').change(function() {
        if ( $(this).val() != 3 )
        {
            $('input[name=someTextbox]').prop('disabled', true);
        }
        else
        {
            $('input[name=someTextbox]').prop('disabled', false);
        }
    });
});

Open in new window


Obviously change the name=xxx part to match your page.
Michel PlungjanIT Expert
Top Expert 2009

Commented:
Chris, please

            $('input[name=someTextbox]').prop('disabled', $(this).val() != 3);
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Nice :)

Author

Commented:
I used id  and name in textbox but I cant get thus to work as when i click the radio button nothing changes.
I changed to radio button

   echo $this->Form->input('startDate',array('id'=>'startdatebox','label' => 'Start Date','class'=>'datepicker', 'type'=>'text','style'=>'width:100px;height:30px','value' => $datestart));
         echo '</td>';
             echo '<td>';
          echo $this->Form->input('endDate',array('id'=>'enddatebox','label' => 'End Date','class'=>'datepicker', 'type'=>'text','style'=>'width:100px;height:30px','value' => $dateend));
             echo '</td>';    
       
  echo $this -> Form -> input('dateRange', 
               array('id'=>'dateRange','label' => '<h6>Date Range</h6>','type' => 'radio', 'value' =>$dateSelect,'options' =>  $selectoption));
     

<script type="text/javascript">  
$(document).ready(function() {
    $('#dateRange').change(function() {
        if ( $(this:'checked').val() != 3 )
        {
            $('#startdatebox]').prop('disabled', true);
            $('#enddatebox]').prop('disabled', true);
            alert('asdsad');
            
        }
        else
        {
            $('#startdatebox]').prop('disabled', false);
            $('#enddatebox]').prop('disabled', false);
             alert('asdsad');
        }
    });
});
</script>

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
You don't need to use 'checked' in the selector (and you're doing it wrong anyway.)

You also have a trailing ] on the end of all your selectors, which is wrong:

$('#dateRange').change(function() {
    $('#startdatebox, #enddatebox').prop('disabled', $(this).val() != 3);
});

Open in new window

Author

Commented:
Sorry it isnt working.
I need an if statement as I need to toggle the textbox.
I am not getting this with your code.


$(document).ready(function() {
    $('#dateRange').change(function() {
        if ( $(this:).val() != 3 )
        {
           $('#startdatebox, #enddatebox').prop('disabled', $(this).val() != 3); // i know this isnt correct what I did
            alert('asdsad');
            
        }
        else
        {
            $('#startdatebox]').prop('disabled', false);
            $('#enddatebox]').prop('disabled', false);
             alert('asdsad');
        }
    });
});

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
You don't need an if statement.  My code will toggle the 2 text boxes just fine.  Try my code as it is.
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Here's a working demo to show it working:

http://jsfiddle.net/ChrisStanyon/zjrm35u4/

Author

Commented:
No doesnt work sorry. I am using cakephp and also radio buttons not a drop down box which is probably the issue.

  $selectoption=array(1=>'Today',0=>'Fortnight',2=>'Monthly',3=>'Custom Range'); 
       echo $this -> Form -> input('dateRange', 
               array('id'=>'dateRange','label' => '<h6>Date Range</h6>','type' => 'radio', 'value' =>$dateSelect,'options' =>  $selectoption));
    

Open in new window

Michel PlungjanIT Expert
Top Expert 2009

Commented:
Please show the HTML that is rendered as (view-source) since it seems you used dropdowns before and not radio?

A statement that can be true or false can replace an if.

$('#startdatebox, #enddatebox').prop('disabled', $(this).val() != 3);

Open in new window



is the exact same as
if ($(this).val() != 3) {
  $('#startdatebox').prop('disabled',true);
  $('#enddatebox').prop('disabled',true);
}

Open in new window

Author

Commented:
Here is a the view source,
Yes I had to change to radio buttons and my original was drop down
<div class="input radio">
    <fieldset>
        <legend>DateRange</legend>
        <input type="radio" name="data[User][dateRange]" id="dateRange1" value="1" />
        <label for="dateRange1">Today</label>
        <input type="radio" name="data[User][dateRange]" id="dateRange0" value="0" checked="checked" />
        <label for="dateRange0">Fortnight</label>
        <input type="radio" name="data[User][dateRange]" id="dateRange2" value="2" />
        <label for="dateRange2">Monthly</label>
        <input type="radio" name="data[User][dateRange]" id="dateRange3" value="3" />
        <label

Open in new window

Michel PlungjanIT Expert
Top Expert 2009

Commented:
Please see how I formatted the code and wrapped it in [ code ] tags (highlight and click the CODE button in the editor.

So if I understand correctly you want to disable some text field when the range is NOT monthly?

$("input[name='data[User][dateRange]']").on("click",function() {
  $('#startdatebox, #enddatebox').prop('disabled', $(this).val()  != 3);
});

Open in new window

Author

Commented:
Hi Michael,

Actually option4 on radio button but that doesn't matter. I am using radio buttons

I would prefer a function and the use of ID's .
I want to check  radio button 4 and 2 text fields to enable and check the 3 others and the text field disable.

Author

Commented:
Hi,

Sorry again but this DOESNT work .

$("input[name='data[User][dateRange]']").on("click",function() {
  $('#startdatebox, #enddatebox').prop('disabled', $(this).val()  != 3);
});
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
The code works absolutely fine. If it's not working for you then something else is going on. What exactly DOESN'T work.

Here's a working demo: http://jsfiddle.net/ChrisStanyon/0m152t1p/
IT Expert
Top Expert 2009
Commented:
Here is an even MORE obvious version: http://jsfiddle.net/mplungjan/jnyeh7n5/

$(function() {
  $("input[name='data[User][dateRange]']").on("click",function() {
    var val = $(this).val();
    $('#startdatebox, #enddatebox').each(function() {
        if (val != 3) $(this).val(""); // empty 
        $(this).prop('disabled', val != 3); // disable or enable
        $(this).toggle(val== 3); // hide or show
    }); 
  }); 
  $("input[name='data[User][dateRange]']:first").click(); // can be changed to click the current selected radio when loading
});

Open in new window

Author

Commented:
I added this code to call the fun and it worked
 'onclick'=> 'myFunc(this.value)'


   function myFunc(val) {
    if(val != 3) {
        document.getElementById("startdatebox").disabled = true;
        document.getElementById("enddatebox").disabled = true;
    } else {
        document.getElementById("startdatebox").disabled = false;
        document.getElementById("enddatebox").disabled = false;
    }

 //  alert( document.getElementById("dateRange").value);
    
}

Open in new window

Michel PlungjanIT Expert
Top Expert 2009

Commented:
Oh well...

Still can be written
function myFunc(val) {
  var disable =  val != 3;
  document.getElementById("startdatebox").disabled = disable;
  document.getElementById("enddatebox").disabled = disable;
}

Open in new window