Skip to content
Advertisement

Javascript to Grab the Selected Value from a list of Form Radio Options OR text field, whichever is used, and output to text?

I’m new to Stack and this is my first question, but I did search and wasn’t able to find anything that was specifically what I am trying to accomplish. I’m all for learning, so any reference material that would help me better understand how to approach this would be appreciated, but if someone wants to provide some example code I wouldn’t mind that either :p

OK, so the scenario is this. I am writing a “Note Creator” that will generate automatic client notes based on some fields I enter into a form. I’ve already scripting getting the values from text fields, but I have one field that I want to be a combination of a radio option OR text field, and the java needs to grab whichever one was used and the proper value. Any help is appreciated, below is my code:

    <script type="text/javascript">
    function getNotes() {
        //Grab Variables
        spokeTo = document.thisForm.spokeWith.value;
        problemItem = document.thisForm.problemWith.value;
        resolvedBy = document.thisForm.resolvedWith.value;
        thisTech = document.thisForm.techName.value;
        fSpace = "... "
        //Read in the location information and prep the output container
        outputValue = "";
        {
        //Test if things are blank
        if (spokeTo == "") { alert('Please Enter the Name of the Person.'); }
        else if (problemItem == "") { alert('Please Select the Problem.');  }
        else if (resolvedBy == "") { alert('Please Type Your Resolution.'); }
        else {
            //The loop that puts the output together
                outputValue += "Spoke With: " + spokeTo + "nn" + "Called About: " + problemItem + "nn" + "Resolution: " + resolvedBy +fSpace + thisTech;
            }
            //output to the user
            document.thisForm.outputArea.value = outputValue;
        }
    }
</script>

        <form name="thisForm">
        <p>
        1. Spoke With: <input type="text" id="spokeWith" class="input" name="spokeWith">
        </p>

        <p>
        2. Called About: 
            Hardware <input type="radio" id="problemWith" class="input" name="problemWith" value="Hardware">
            Software <input type="radio" id="problemWith" class="input" name="problemWith" value="Software">
            <input type="text" id="problemWith" class="input" name="problemWith"><br>
        </p>

        <p>
        3. Resolved By:<br /><br />
            <textarea name="resolvedWith" id="resolvedWith"></textarea>
        </p>

        <p>
        4. Your Name:<br>
            <input type="text" id="techName" class="input" name="techName" /><br>
        </p>

        <p>
            <input type="button" class="button" value="Make My Notes!" onClick="javascript:getNotes();" />
            <input type="reset" class="button" value="Start Over" />
        </p>

        <br><br>

        <textarea name="outputArea" id="outputArea"></textarea>
        <p class="finishMessage" id="finishMessage" name="finishMessage"></p>
    </form>

I am referring specifically to The Step 2 section of the form where it has radio options and a text field with the same IDs and names. I know IDs are only supposed to be used once per page so this would probably change but I’m open to any suggestion/assistance. I’m moderate with Javascript, still in the learning phases.

Thanks again!

————— ROUND 2 —————

Here is my revised code after taking the suggestion of the provided answer. I added a bunch of alerts to kind of let me know along the way which parts of the script I’m managing to hit, and I can’t get anything past the first alert to trigger, and can’t get any output at all anymore. What am I missing?

<script type="text/javascript">
    function getNotes() {
    alert('You've hit the function. Congratulations - you didn't break the whole damn thing.');
        //Grab Variables
        var spokeTo = document.thisForm.spokeWith.value;
        var resolvedBy = document.thisForm.resolvedWith.value;
        var thisTech = document.thisForm.techName.value;
        var fSpace = "&hellip; ";

        //Grab if radio else text
        var inputVal1 = document.getElementByClass('problemRadio').value;
        var inputVal2 = document.getElementByClass('problemWith').value;
        alert('You made it! Almost there.');
        // If Input Value 1 is not Null, Show Radio Value. Else, show Text Value
        var problemOutput;
        if (inputVal1.length > 0) {
        problemOutput = inputVal1;
        alert('I found value 1!');
        }
        else {
        problemOutput = inputVal2; 
        alert('I found value 2!'); 
        }

        //Read in the location information and prep the output container
        outputValue = "";

        //Test if things are blank
        if (spokeTo == "") { alert('Please Enter the Name of the Person.'); }
        else {
        alert('We Made it to Else to parse outputValue');
            //The loop that puts the output together
                outputValue += "Spoke With: " + spokeTo + "nn" + "Called About: " + problemOutput + "nn" + "Resolution: " + resolvedBy +fSpace + thisTech;
            }
            //output to the user
            document.thisForm.outputArea.value = outputValue;

    }
</script>

        <form name="thisForm">
        <p>1. Spoke With: <input type="text" id="spokeWith" class="input" name="spokeWith"></p>

        <p>2. Called About: 
            Hardware <input type="radio" id="problemRadio" class="problemRadio" name="problemRadio" value="Hardware">
            Software <input type="radio" id="problemRadio" class="problemRadio" name="problemRadio" value="Software">
            <input type="text" id="problemWith" class="problemWith" name="problemWith"><br>
        </p>

        <p>3. Resolved By:<br /><br />
            <textarea name="resolvedWith" id="resolvedWith"></textarea>
        </p>

        <p>4. Your Name:<br>
            <input type="text" id="techName" class="input" name="techName" /><br>
        </p>

        <p>
            <input type="button" class="button" value="Make My Notes!" onClick="javascript:getNotes();" />
            <input type="reset" class="button" value="Start Over" />
        </p>

        <br><br>

        <textarea name="outputArea" id="outputArea"></textarea>
        <p class="finishMessage" id="finishMessage" name="finishMessage"></p>
    </form>

Advertisement

Answer

sorry about that, I don’t know the best way to get code in here yet. Always ends up messy looking ( till I figure it out )

Few things I spotted in your update;

1) getElementbyId is best ( it wasn’t getting past your first lines)

2) duplicate IDs on the radio buttons

3) We Needed to ‘check’ which of the radio buttons was checked

4) always handy to alert the variables ( I’ve added some in to show )

I gave this code below a rough test and it looks to be along the lines ..

see comments added in your code below ..

 <script type="text/javascript">
   function getNotes() {

    //Grab Variables
    var spokeTo = document.getElementById('spokeWith').value;
    var resolvedBy = document.getElementById('resolvedWith').value;
    var thisTech = document.getElementById('techName').value;
    var fSpace = "&hellip; ";

    alert("spokeTo:" + spokeTo 
      +" , resolvedBy: " +resolvedBy+", thisTech: "+thisTech);

    //Grab if radio else text

    var problemWith = document.getElementById('problemWith').value;

   alert("problemWith: "+problemWith);

/* set a default value */
var problemOutput="other";

/* if they added no notes */
if((problemWith=="") || ( problemWith==null)) {

/* check which radio is selected if any */
if(document.getElementById('problemHardware').checked) {
problemOutput = document.getElementById('problemHardware').value;
}

if(document.getElementById('problemSoftware').checked) {
problemOutput = document.getElementById('problemSoftware').value;
}

} else {
problemOutput=problemWith;
}
alert("problem output is: "+problemOutput);

    alert('You made it! Almost there.');


    //Read in the location information and prep the output container
    outputValue = "";

    //Test if things are blank
    if (spokeTo == "") { alert('Please Enter the Name of the Person.'); }
    else {
    alert('We Made it to Else to parse outputValue');
    /* The loop that puts the output together */

    outputValue += "Spoke With: " 
     + spokeTo + "nn" 
     + "Called About: " 
     + problemOutput + "nn" 
     + "Resolution: " + resolvedBy +fSpace + thisTech;
        }
        //output to the user
        document.thisForm.outputArea.value = outputValue;

}
  </script>

      <form name="thisForm">
    <p>1. Spoke With: <input type="text" id="spokeWith" class="input" name="spokeWith">  </p>

    <p>2. Called About: 
        Hardware <input type="radio" id="problemHardware" class="problemRadio" name="problemRadio" value="Hardware">
        Software <input type="radio" id="problemSoftware" class="problemRadio" name="problemRadio" value="Software">
        <input type="text" id="problemWith" class="problemWith" name="problemWith"><br>
    </p>

    <p>3. Resolved By:<br /><br />
        <textarea name="resolvedWith" id="resolvedWith"></textarea>
    </p>

    <p>4. Your Name:<br>
        <input type="text" id="techName" class="input" name="techName" /><br>
    </p>

    <p>
        <input type="button" class="button" value="Make My Notes!" onClick="javascript:getNotes();" />
        <input type="reset" class="button" value="Start Over" />
    </p>

    <br><br>

    <textarea name="outputArea" id="outputArea"></textarea>
    <p class="finishMessage" id="finishMessage" name="finishMessage"></p>
</form>
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement