I'm trying to do is hiding/showing a certain input object if a select value is checked.
The HTML part of the code is here:
<label for="add_fields_placeholder">Placeholder: </label>
<select name="add_fields_placeholder" id="add_fields_placeholder">
<option value="50">50</option>
<option value="100">100</option>
<option value="Other">Other</option>
</select>
<div id="add_fields_placeholderValue">
Price:
<input type="text" name="add_fields_placeholderValue" id="add_fields_placeholderValue">
</div>
And the jQuery part is here:
$(document).ready(function()
{
$("#add_fields_placeholder").change(function() {
if($(this).val() == "Other") {
$("#add_fields_placeholderValue").show();
}
else {
$("#add_fields_placeholderValue").hide();
}
});
});
So if user selects "Other", it shows another input object.
The problem now is this. First when you open the page the first option is selected by default and the optional input object is shown. It hides once you select another option.
Is there any trick to make it hide when you first load the page too? Not just when you select a value manually.
Thank you guys.
You can use css to hide it initially
http://jsfiddle.net/FarVX/20/
Also you have multiple elements with the same id(pointed out by Brandon), which is not valid
I typically factor out the hide/show logic:
and call it when the page loads.
but i'd be curious to see what other folks usually do.
if you change the anonymous method into a callable function, you should be able to call it on Ready()
e.g.
Just add:
After your change event declaration on page load.
i.e.
Working example: http://jsfiddle.net/bZXYR/
You could simply do it with CSS:
Change the ID here:
since you already use it here
and then add this css:
Place the following code beneath the placeholder elements:
Doing it in the ready handler may induce 'flashing' of the element in certain circumstances:
Twinkling when call hide() on document ready