Reputation: 249
HTML:
<input type="radio" name="timeformat" id="timeformat-a" value="am/pm" checked="checked" /> <b>am/pm</b>
<input type="radio" name="timeformat" id="timeformat-b" value="24 hours" /> <b>24 hours</b>
<select id="default-start-time" class="select check-list">
<option value="" label="--Select--">--Select--</option>
<option value="00:00" label="12:00 AM">12:00 AM</option>
<option value="00:30" label="12:30 AM">12:30 AM</option>
<option value="01:00" label="01:00 AM">01:00 AM</option>
<option value="01:30" label="01:30 AM">01:30 AM</option>
<option value="02:00" label="02:00 AM" selected>02:00 AM</option>
<option value="02:30" label="02:30 AM">02:30 AM</option>
</select>
When i choose the 24 hours format the option values should display.
jquery:
$("input[name='timeformat']").click(function() {
if($(this).val()=='24 hours')
{
// code which replaces label with the option value
}
});
How should the jquery look??
Upvotes: 0
Views: 3451
Reputation: 9792
I advise doubling the HTML and simplify the JS:
HTML:
<input type="radio" name="timeformat" value="12" checked="checked" /> <b>am/pm</b>
<input type="radio" name="timeformat" value="24" /> <b>24 hours</b>
<select id="format-12" class="">
<option value="" selected>--Select--</option>
<option value="12:00 AM">12:00 AM</option>
<option value="12:30 AM">12:30 AM</option>
<option value="01:00 AM">01:00 AM</option>
<option value="01:30 AM">01:30 AM</option>
<option value="04:00 PM">04:00 PM</option>
<option value="05:30 PM">05:30 PM</option>
</select>
<select id="format-24" class="hide">
<option value="" selected>--Select--</option>
<option value="00:00">00:00</option>
<option value="12:30">12:30</option>
<option value="01:00">01:00</option>
<option value="01:30">01:30</option>
<option value="16:00">16:00</option>
<option value="17:30">17:30</option>
</select>
JS:
$("input[name='timeformat']").on('change', function(){
$('select[id*=format-]').toggleClass('hide');
});
CSS:
.hide { display: none; }
DEMO: http://jsfiddle.net/9ELJj/
Upvotes: 0
Reputation: 340055
If you have both a label
attribute and text content in an <option>
element the <label>
will take priority.
You can copy the values into the label like this:
$('#default-start-time option').attr('label', function() {
return this.value;
});
similarly you can copy the text content back into the label like this:
$('#default-start-time option').attr('label', function() {
return this.innerHTML;
});
See http://jsfiddle.net/alnitak/cLuZa/
Note that (in Chrome, at least) neither will cause the control to change its current displayed value. They only appear when you click on the <select>
element again. I'm still working on that...
EDIT here's my final answer:
$("input[name='timeformat']").click(function() {
var ampm = $(this).val() !== '24 hours'; // get mode
var sel = $('#default-start-time')[0]; // get the <select> element
$('option', sel).attr('label', function() {
return ampm ? this.innerHTML : this.value;
});
sel.selectedIndex += 0; // force refresh
});
Upvotes: 1
Reputation: 50808
Changing the states of the label based on the value selected is going to change it permanently. We need to store a reference to each the values we want to toggle inside of something that's easy to work with, so we use data
attributes.
I don't know if you have access to the HTML, so here's the jQuery to do what we need.
$('#default-start-time option').each(function(index,obj){
$(obj).data('ampm', $(obj).prop('label'));
$(obj).data('24hr', $(obj).val());
});
Now, in the click function, we can toggle between the data attributes based on the value selected.
$("input[name='timeformat']").click(function() {
if($(this).val()=='24 hours')
{
$('#default-start-time option').each(function(index,obj){
$(obj).prop('label', $(obj).data('24hr'));
});
}
else
{
$('#default-start-time option').each(function(index,obj){
$(obj).prop('label', $(obj).data('ampm'));
});
}
});
Here is the fully working jsFiddle.
Upvotes: 1