Skip to main content
Inspiring
November 1, 2011
Answered

select Drop-Down List item Based On Text Field not being empty

  • November 1, 2011
  • 2 replies
  • 2466 views

Hi. I have some javascript code to click on a button and it will insert the date into a text box called "Controller_PDF_Creation_Doc_Control_Date" on my form. I also have a drop down menu called "Release_Approval_Initials". All I'm trying to do is when there is a date entered into the text box, I want the "Release_Approval_Initials" drop down menu to default by selecting the option "TM". The drop down menu is dynamically populated too. How can I do this? Below is the code that I have.

<SCRIPT LANGUAGE="JavaScript">

    function verify() {

        var partNumber = '';

        var ecoNumber = '';

        var pdfDate;

        var queue;

        var queueValue='';

        var allArray = document.getElementById('listofids').value.split(",");

        var error=false;

       

        for(var i=0;i<allArray.length;i++) {

            pdfDate = document.getElementById('Controller_PDF_Creation_Doc_Control_Date'+allArray).value;

             <!--- document.getElementById('Controller_PDF_Creation_Doc_Control_Date1').value='8/19/11'; --->

            queue = document.getElementById('Release_Approval_Initials'+allArray);

            queueValue = queue[queue.selectedIndex].value;

           

            if((pdfDate != '' && queueValue == '') || (pdfDate == '' && queueValue != '')) {

                error=true;

                ecoNumber = document.getElementById('ECID'+allArray).value;

                partNumber = document.getElementById('Part_Number'+allArray).value;

                alert("You must enter both a PDF Creation and/or Doc Control approval date and Queue to Release for ECO " + ecoNumber + " Part Number: " + partNumber);

            }

        }

       

        if(error) {

            return false;

        }

        else {

            return true;

        }

    }

</script>

<cfinput type="Text" name="Controller_PDF_Creation_Doc_Control_Date#ItemID#" id="Controller_PDF_Creation_Doc_Control_Date#ItemID#" value="#DateFormat(Controller_PDF_Creation_Doc_Control_Date,"M/D/YY")#" size="12" maxlength="8" validate="date" required="no" message="You must enter a valid date in M/D/YY format in the PDF Creation and/or Doc Control Date field">

<cfinput type="hidden" name="today_date" id="today_date" value="#DateFormat(now(),"M/D/YY")#" />

<input type= "button" value="Today's Date" onclick="document.getElementById('Controller_PDF_Creation_Doc_Control_Date#ItemID#').value=document.getElementById('today_date').value">

<select name="Release_Approval_Initials#ItemID#" id="Release_Approval_Initials#ItemID#">

                <option value=""></option>

                <cfloop query="ShowDataEntryInitials">

                    <option value="#Initials#"

                    <cfif Initials EQ ReleaseInitials>selected

                    </cfif>>#Initials#</option>

                </cfloop>

            </select>

Any help would be greatly appreciated. Thank you.

Andy

    This topic has been closed for replies.
    Correct answer jamie61880

    I figured out how to get this to work. I just had to add in some parenthesis in the correct places to make it work. Here's what I did:

    <cfinput type="Text" name="Controller_PDF_Creation_Doc_Control_Date#ItemID#" id="Controller_PDF_Creation_Doc_Control_Date#ItemID#" value="#DateFormat(Controller_PDF_Creation_Doc_Control_Date,"M/D/YY")#" size="12" maxlength="8" validate="date" required="no" message="You must enter a valid date in M/D/YY format in the PDF Creation and/or Doc Control Date field" onclick="document.getElementById('Release_Approval_Initials#ItemID#').value = 'TM'">

    <cfinput type="hidden" name="today_date" id="today_date" value="#DateFormat(now(),"M/D/YY")#" />

    <input type= "button" value="Today's Date" onclick="(document.getElementById('Controller_PDF_Creation_Doc_Control_Date#ItemID#').value=document.getElementById('today_date').value) && (document.getElementById('Release_Approval_Initials#ItemID#').value = 'TM')">

    2 replies

    jamie61880AuthorCorrect answer
    Inspiring
    November 3, 2011

    I figured out how to get this to work. I just had to add in some parenthesis in the correct places to make it work. Here's what I did:

    <cfinput type="Text" name="Controller_PDF_Creation_Doc_Control_Date#ItemID#" id="Controller_PDF_Creation_Doc_Control_Date#ItemID#" value="#DateFormat(Controller_PDF_Creation_Doc_Control_Date,"M/D/YY")#" size="12" maxlength="8" validate="date" required="no" message="You must enter a valid date in M/D/YY format in the PDF Creation and/or Doc Control Date field" onclick="document.getElementById('Release_Approval_Initials#ItemID#').value = 'TM'">

    <cfinput type="hidden" name="today_date" id="today_date" value="#DateFormat(now(),"M/D/YY")#" />

    <input type= "button" value="Today's Date" onclick="(document.getElementById('Controller_PDF_Creation_Doc_Control_Date#ItemID#').value=document.getElementById('today_date').value) && (document.getElementById('Release_Approval_Initials#ItemID#').value = 'TM')">

    Inspiring
    November 2, 2011

    Baby steps. 

    Start with a text box and a select with two options.  Write a function that picks the second option when the text box has something in it.  Once you have that working, start adding the rest of your stuff.

    If you need help writing the function, your google search string is "set dropdown selected value using javascript".

    Inspiring
    November 2, 2011

    Dan,

         I can get this to work if I click in the text box, it will select "TM" in the drop down menu. Here's my code for this:

    <cfinput type="Text" name="Controller_PDF_Creation_Doc_Control_Date#ItemID#" id="Controller_PDF_Creation_Doc_Control_Date#ItemID#" value="#DateFormat(Controller_PDF_Creation_Doc_Control_Date,"M/D/YY")#" size="12" maxlength="8" validate="date" required="no" message="You must enter a valid date in M/D/YY format in the PDF Creation and/or Doc Control Date field" onclick="document.getElementById('Release_Approval_Initials#ItemID#').value = 'TM'">

         But how can I make the button that put's today's date into the text box, also select "TM" in the drop down menu? Can this one button insert the date into the text box and also select "TM" in the drop down menu? If so, how do you do that? I just tried using the && symbols, but that doesn't work. Here's what I have for this, but it's not working:

    <cfinput type="hidden" name="today_date" id="today_date" value="#DateFormat(now(),"M/D/YY")#" />

    <input type= "button" value="Today's Date" onclick="document.getElementById('Controller_PDF_Creation_Doc_Control_Date#ItemID#').value=document.getElementById('today_date').value" && "document.getElementById('Release_Approval_Initials#ItemID#').value = 'TM'">

    Thanks.

    Andy