views:

79

answers:

3

I have two input fields, Id and Desc. I need a javascript which enters the values in second text field based on some of the fields already set. Like for christmas day, if 12/25 is entered, it should say Christmas day.

Its working for one value. But if I put "else if" its not working for multiple values.

[code] <script type="text/javascript"> $(document).ready(function() { $('#holidayDate').datepicker(); var availableTags = ["New years Day", "Martin Luther King Day", "Groundhog Day", "Valentine's Day", "Washington's Birthday", "Easter", "Earth Day", "National Arbor Day", "Mother's Day", "Memorial Day", "Flag Day", "Father's Day", "Independence Day", "Labor Day", "Columbus Day", "Halloween", "Veterans Day", "Thanksgiving Day", "Pearl Harbor Remembrance Day", "Christmas Day"]; $("#tags").autocomplete({source:availableTags}); $('#holidayDate').change(function() { if ($(this).val().substring(0, 5) === '12/25') { $('#tags').val('Christmas Day'); }else if ($(this).val().substring(0, 5) === '01/01') { $('#tags').val('New years Day'); } else { $('#tags').val(''); }
}); }); </script>
[/code]

+2  A: 

First, you can combine those into one DOM ready event. Also, some of your code was outside the event, so that's probably why it wasn't working.

$(function() {
    $('#date').datepicker();
    var availableTags = ["Christmas Day","etc"];
    $("#tags").autocomplete({source:availableTags});
    $('#date').change(function() {
        if ($(this).val().substring(0, 5) === '12/25') {
            $('#desc').val('Christmas');
        } else {
            $('#tags').val('');
        }
    });
});

UPDATE: Hopefully it's just a poorly written example, but your given HTML is very invalid as well. Notice you are opening a <div> tag and then closing a <p> tag, among other issues like not closing your <head> tag before opening the body, etc. All of these issues could be preventing the javascript from functioning properly.

Stephen
Awesome..thanks
oneofthelions
No problem. You're welcome.
Stephen
A: 

I believe you're missing an apostrophe in the line that says

$('#desc).val('Christmas');

it should read

$('#desc').val('Christmas');
Josiah
A: 

Maybe you should do this:

<DIV class="ui-widget">Date: <INPUT TYPE="TEXT" name="xx" property="xx" id="date" /></P>
<input type="text" id="desc" name="desc"/>
<script>
$('#date').change( function() { 
    if ($(this).val().substring(0, 5) === '12/25') { 
        $('#desc').val('Christmas'); 
    } 
});   
</script>
</body>

I think your onchange event is not being registered.

Regards, Satyajit

Satyajit