Reputation: 8836
I have the following scripts that it puts the data-length and data-width on textbox based on drop down question
My question is how I make the .width
and .length
textboxes readonly based on dropodown selection as below?
$(document).ready(function() {
$('select.cargo_type').change(function() {
var eur1width = $('select.cargo_type').find(':selected').data('width');
$('.width').val(eur1width);
//make width textbox readonly
var eur1length = $('select.cargo_type').find(':selected').data('length');
$('.length').val(eur1length);
//make length textbox readonly
});
});
Upvotes: 0
Views: 562
Reputation: 458
Use prop('readonly',true/false);
on the element. Like,
if(eur1width == 'checking_val'){
$('.width').val(eur1width).prop('readonly', true);
}else{
$('.width').val(eur1width).prop('readonly', false);
}
To toggle the property
$('.width').val(eur1width).prop('readonly', !$(this).prop('readonly'));
Upvotes: 1
Reputation: 87203
You can use prop
to set textbox readonly
.
$('.width').val(eur1width).prop('readonly', true);
$('.length').val(eur1length).prop('readonly', true);
To remove readonly
you can set the readonly
property to false;
$('.width').val(eur1width).prop('readonly', false);
$('.length').val(eur1length).prop('readonly', false);
UPDATE
if (eur1width == 'myVal') {
$('.width').val(eur1width).prop('readonly', true);
} else {
$('.width').val(eur1width).prop('readonly', false);
}
Upvotes: 1