how to disable option in drop down box using code igniter?i want to disable the value"------"in this drop down
echo "<tr class='background1'><td class='checkoutfield'>";
$countryall='';
$select='';
if(isset($order)) $country=$order['varShippingCountry']; else $country='';
if(isset($countries) && $countries !='') {
$countryall['']="Select";
foreach($countries as $key=>$value):
$countryall["226/United States"]="United States";
if($value['id'] !='226') {
//<option value=”spider” disabled=”disabled”>Spider</option>
$countryall['0']="-------------------------------------------------------";
$countryall["$value[id]/$value[varPrintableName]"]=$value['varPrintableName'];
}
if($value['id'] == $country)
$select="$value[id]/$value[varPrintableName]";
endforeach;
}
$selFunc='style="width:190px;" id="varShippingCountry" class="required" onchange="stateajax(this.value)" onKeyup="return changeText(\'varShippingCountry\',\'varPaymentCountry\',\'this.value\')"';
echo form_label('Country','varShippingCountry')."<span class='mandatory'>*</span></td><td>";
echo form_dropdown('varShippingCountry',$countryall,$selFunc);
You can't disable an item in a HTML select element - only disable the entire element.
Are you trying to make a separator between different sections of the list? In that case, you can use <optgroup>.
<select>
<optgroup label="Swedish Cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
</optgroup>
<optgroup label="German Cars">
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</optgroup>
</select>
You can make this happen in CodeIgniter by passing it a multidimensional array.
Alternatively, if you are just trying to make the first and default item look like ------, then add it in the normal way with an empty value. Then on validation, check that an empty value has not been passed.
Related
I'm trying to dynamically the select option selected just using php in laravel but I'm getting this error:
Use of undefined constant selected - assumed 'selected' (this will
throw an Error in a future version of PHP) (View:
C:\xampp\htdocs\laralast\resources\views\view.blade.php)
below is my view blade
<select class="form-control" name="assign_to" id="assign_to">
<option selected disabled>Select support</option>
#foreach($supports as $support)
<option value="{{$support->id}}" {{($ticket->assign_by == $support->id ? selected : '')}}>{{$support->fullname}}</option>
#endforeach
</select>
Can you help me with this.
You should quote your string (I mean you should quote the selected):
<option value="{{$support->id}}" {{($ticket->assign_by == $support->id ? 'selected' : '')}}>{{$support->fullname}}</option>
Your laravel is understanding that you will print the value of selected constant (since no dollar-sign $, no string quotation '' "") when the condition is true.
Please Check This use if & else
<select class="form-control" name="assign_to" id="assign_to">
<option selected disabled>Select support</option>
#foreach($supports as $support)
#if($ticket->assign_by == $support->id)
<option value="{{$support->id}}" selected>{{$support->fullname}}
</option>
#else
<option value="{{$support->id}}">{{$support->fullname}}</option>
#endforeach
</select>
Is it possible to send value from options on example like this where x need to be value from option. This select im using on partial and ajax works with manualy added value as x. THX!
<select data-request-data = "id: x " class="form-control custom-select" data-request="onChangeValue">
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
Just give your select a name, an unique name.
name="some_unique_name"
<select name="my_super_special_name_for_select" class="form-control custom-select" data-request="onChangeValue">
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
</select>
Then when you change the value the value of the field is sent along in post
The result of:
public function onChangeValue()
{
traceLog(post());
}
In the log you will see then a result corresponding to
["my_super_special_name_for_select" => 5 ]
So you can fetch it with post('my_super_special_name_for_select') or whatever name you have given the select element to get the value.
public function onChangeValue()
{
$id = post('my_super_special_name_for_select');
}
Yes you can send value from select. But for this you will need to put it inside a form and then call data request on it. Value will be sent as part of post(). It can't be sent dynamically like the way you are using. if you can elaborate your external conditions more I can suggest other solutions too.
I have three dropdowns with same class name:
<select class="MyClass">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<select class="MyClass">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<select class="MyClass">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
I want to change any of these dropdown values based on any other dropdrop selected. If I select option two from second dropdown - I want 1st and 3rd dropdown values to be two. All these dropdown should change values no matter which one I select.
Thanks.
You will need JavaScript to do this. Here is an code example using jQuery:
$('.MyClass').on('change',function(){
$('.MyClass').val( $(this).val() );
});
This will add an EventListeneer for the change Element and update all DropDowns to the value of the changed one.
http://jsfiddle.net/CWP7Q/
you can use jQuery or normal JS, but my example contains Jquery, which will give you an idea. Please check the example in Jsfiddle.
The example binds a change event to the class name of the three selectors:
$(".MyClass").change(function(){
$(".MyClass").val($(this).val());
});
and Voila, every selector gets changed.
Somehting like (with jQuery)
$('.MyClass').change(function() {
$('.MyClass').val($(this).val());
});
I have a select list say of states in the country, which i have in a helper to include easily in any form. (removing most options to make it brief).
I have the value of the current selection stored in the database say "CA". How would i set selected="true" to option CA before rendering the list to the user?
#helper StateSelect(string name = "State")
{
<select name="#name" id="#name" class="required">
<option value="">-- Select -- </option>
<option value="AK">Alaska</option>
<option value="AL">Alabama</option>
<option value="AR">Arkansas</option>
<option value="AZ">Arizona</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="VA">Virginia</option>
<option value="VT">Vermont</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select>
}
As Darin Dimitrov says, the built-in stuff would be better. However, if you do need to, I think you have a few options:
Add code like this to every line:
<option value="CA" #(name == "CT" ? "selected=selected" : "")> Connecticut</option>
Just re-add the selected item to the top of the list
This keeps the code cleaner, the selected option is just repeated at the top (and selected there), before the entire list
Sorry guys, I should have posted the script directly without cleaning it. Please check the updated script, it should now clear things up. Thanks!
Consider the following JavaScript :
var selected = 0;
var options = 0;
$('.newListSelected').each(function() {
selected = $(this).children('.selectedTxt');
selected = selected.text();
/* Everything before this line works completely fine */
options = $(this).prev();
options.find('option[value=' +selected+ ']').attr('selected', 'selected');
}).remove();
And HTML :
<select name="type[]" style="display: none;">
<optgroup>
<option value="none">Select</option>
</optgroup>
<optgroup label="First Group">
<option value="1">One</option>
<option value="2">Two</option>
</optgroup>
<optgroup label="Second Group">
<option value="10">Ten</option>
<option value="20">Twenty</option>
</optgroup>
</select>
<div class="newListSelected">
<div class="selectedTxt">20</div>
<ul class="newList">
<!-- Other stuff here -->
</ul>
</div>
What I'm trying to do actually is adding the selected attribute to the corresponding select option that has the same value as the text in .selectedTxt. In the code above, it should add selected="selected" to <option value="20">Twenty</option>.
However its not performing as expected, I also tried adding alert(option); below prev(); but it didn't output anything useful.
Thanks.
The Javascript works fine, as can be seen here: http://jsfiddle.net/4HsXp/
If you need to be able to select multiple items in a select element, you need to set the multiple and size attribute, like this:
<select multiple="multiple" size="2">
<option>1</option>
<option>2</option>
</select>
See: http://reference.sitepoint.com/html/select
Edit:
Attaching console.log statements to the new code still does not any problem. Running it on jsfiddle gave me the correct output:
jQuery(select)
Original Value: none
New Value: 20
I'm not sure what you are trying to do, but why couldn't you just select all option elements with proper selectors? If you want to select only some options, then you have to add extra selector attrbiutes.
$('select option').each(function() {
$(this).attr('selected', 'selected');
});