jagguy
asked on
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('dateRa nge', array('label' => 'Date Range',
'options' => $dateRange,'style'=>'width :150px','s elected' => $dateSelect));
//select date
echo $this->Form->input('startD ate',array ('label' => 'Start Date','class'=>'datepicker ', 'type'=>'text','style'=>'w idth:100px ;height:30 px','value ' => $datestart));
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('dateRa
'options' => $dateRange,'style'=>'width
//select date
echo $this->Form->input('startD
Chris, please
$('input[name=someTextbox] ').prop('d isabled', $(this).val() != 3);
$('input[name=someTextbox]
Nice :)
ASKER
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
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>
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:
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);
});
ASKER
Sorry it isnt working.
I need an if statement as I need to toggle the textbox.
I am not getting this with your code.
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');
}
});
});
You don't need an if statement. My code will toggle the 2 text boxes just fine. Try my code as it is.
ASKER
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));
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.
is the exact same as
A statement that can be true or false can replace an if.
$('#startdatebox, #enddatebox').prop('disabled', $(this).val() != 3);
is the exact same as
if ($(this).val() != 3) {
$('#startdatebox').prop('disabled',true);
$('#enddatebox').prop('disabled',true);
}
ASKER
Here is a the view source,
Yes I had to change to radio buttons and my original was drop down
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
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?
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);
});
ASKER
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.
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.
ASKER
Hi,
Sorry again but this DOESNT work .
$("input[name='data[User][ dateRange] ']").on("c lick",func tion() {
$('#startdatebox, #enddatebox').prop('disabl ed', $(this).val() != 3);
});
Sorry again but this DOESNT work .
$("input[name='data[User][
$('#startdatebox, #enddatebox').prop('disabl
});
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/ChrisS tanyon/0m1 52t1p/
Here's a working demo: http://jsfiddle.net/ChrisS
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I added this code to call the fun and it worked
'onclick'=> 'myFunc(this.value)'
'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);
}
Oh well...
Still can be written
Still can be written
function myFunc(val) {
var disable = val != 3;
document.getElementById("startdatebox").disabled = disable;
document.getElementById("enddatebox").disabled = disable;
}
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:
Open in new window
Obviously change the name=xxx part to match your page.