I need to grab the first 4 characters of two input values (if entered), and string them together and insert them into a third input as the default value of the input. I have this working except for grabbing the first four characters (right now it’s grabbing the whole value, which could be a quite long airport name).
js fiddle: http://jsfiddle.net/4byPJ/
//insert to and from values as the default seach name
$("#from").change(function() {
$("#nameSearch").val($("#from").val() + " to " + $("#to").val());
});
$("#to").change(function() {
$("#nameSearch").val($("#from").val() + " to " + $("#to").val());
});
<fieldset class="itineraryBlock">
<legend>Itinerary</legend>
<div class="fromFields">
<label for="from">From:</label>
<input type="text" name="from" id="from" class="airportField" />
</div>
<div class="toFields">
<label for="to">To:</label>
<input type="text" name="to" id="to" class="airportField" />
</div>
</fieldset>
<fieldset class="saveSearch">
<legend>Do you want to save this search?</legend>
<input type="radio" id="yes" value="yes" name="saveSearch" checked="checked" />
<label for="yes">Yes</label>
<input type="radio" id="no" value="no" name="saveSearch" />
<label for="no">No</label>
<div class="nameSearchBlock">
<label for="nameSearch">Name your search:</label>
<input type="text" id="nameSearch" name="nameSearch" />
</div>
<!-- nameSearch -->
</fieldset>
This should do the trick:
http://jsfiddle.net/4byPJ/3/
You can use the .substring() function to change the string to only include the first 4 characters :).