disable textbox

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));
jagguyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
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 ExpertCommented:
Chris, please

            $('input[name=someTextbox]').prop('disabled', $(this).val() != 3);
Chris StanyonWebDevCommented:
Nice :)
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

jagguyAuthor 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

Chris StanyonWebDevCommented:
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

jagguyAuthor 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

Chris StanyonWebDevCommented:
You don't need an if statement.  My code will toggle the 2 text boxes just fine.  Try my code as it is.
Chris StanyonWebDevCommented:
Here's a working demo to show it working:

http://jsfiddle.net/ChrisStanyon/zjrm35u4/
jagguyAuthor 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 ExpertCommented:
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

jagguyAuthor 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 ExpertCommented:
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

jagguyAuthor 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.
jagguyAuthor Commented:
Hi,

Sorry again but this DOESNT work .

$("input[name='data[User][dateRange]']").on("click",function() {
  $('#startdatebox, #enddatebox').prop('disabled', $(this).val()  != 3);
});
Chris StanyonWebDevCommented:
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/
Michel PlungjanIT ExpertCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jagguyAuthor 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 ExpertCommented:
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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
AJAX

From novice to tech pro — start learning today.