0

I am attempting to build a string of a user's "interests" that they indicate by checking off radio boxes. When I return the result, there is always an "undefined" prepended to the string of interests. I know that I can get rid of this issue by initializing var interest as an empty string, like so:

var interests ="";

But am unsure if this is the proper way to solve the issue. is there a more optimal data structure for this?

        var controlIndex;
        var element;
        var interests;
        var numberOfControls = document.form1.length;

       for (controlIndex = 0; controlIndex < numberOfControls; controlIndex++)

            {
                element = document.form1[controlIndex];
                if (element.type == "radio")
                {
                    if (element.checked == true)
                    {

                     interests += document.form1[controlIndex].value+"\n";


                     console.log(interests);
                     document.getElementById("interests").innerHTML= interests
                    }
                }
            }

        }


    }



<form action="" name="form1">
        <h1>Personal Details</h1>
        Please enter the following details:
        <br>
        <p>
        First Name:
        <br />
        <input type="text" name="txtName"  onchange="txtName_onchange()"/>
        </p>

        <p>
        Age:
        <br />
        <input type="text" name="txtAge" size="3" maxlength="3" onchange="textAge_onblur()" />
        </p>
        <p>
         My interest is:
        <p>Sports
        <input type="radio" name="sports"  value="sports"/>
        </p>
        <p>Politics
        <input type="radio" name="politics" value="politics" />
        </p>
        <p>Magazines
        <input type="radio" name="magazines" value="magazines">
        </p>
        <p>
        <input type="button" value="Submit Registration" name="btnCheckForm"  onclick="btnCheckForm_onclick()" >
        <input type = "button" value = "Clear Details" name="btnClear" onclick="btnClear_onclick()">
        </p>
    </form>
  </div>

3 Answers 3

2

I would turn your "interests" variable into an array.

var interests = [];

then I would just push into it, like so. When you want to print it out, just join it.

             interests.push(document.form1[controlIndex].value);

             console.log(interests.join(""));
Sign up to request clarification or add additional context in comments.

1 Comment

The question is whether using var interests =""; is a proper way to solve the issue. This answer seems to just reflect your own preference.
2

But am unsure if this is the proper way to solve the issue ...

Yes, initialising the variable as a string is the proper way to resolve this issue.

Basically, whenever you initialise your variable like this:

var interests;

The variable type is implicitly set to undefined, so when you apply += onto it, JavaScript changes the type to string with a value of "undefined". Setting the initial value prevents that:

var interests = '';

2 Comments

Jack - thanks for your input - seems there are multiple ways to skin a cat…or at least define the undefined ;)
Yeah; btw, you can change the .innerHTML after the loop is done instead of inside each iteration.
0

I have tried to put same thing mentioned in above answers in code snippet for better understanding. In my opinion array suits here

function btnCheckForm_onclick (){ 
var controlIndex;
        var element;
        //Case with '' intitlization
        var interests = '';
        
        //Case with [] intitlization
        var interests = [];
        
       var numberOfControls = document.form1.length;

       for (controlIndex = 1; controlIndex < numberOfControls; controlIndex++)

            {
                element = document.form1[controlIndex];
                if (element.type == "radio")
                {
                    if (element.checked == true)
                    {
                     // Case with []
                     //interests.push(document.form1[controlIndex].value);
                     //console.log(interests.join(" "));
                    
                     //Case with ''  
                     interests += document.form1[controlIndex].value+"\n";
                     console.log(interests);
                     
                    }
                }
            }
  
  document.getElementById("interests").innerHTML= interests

}
<form action="" name="form1">
        <h1>Personal Details</h1>
        Please enter the following details:
        <br>
        <p>
        First Name:
        <br />
        <input type="text" name="txtName"  onchange="txtName_onchange()"/>
        </p>

        <p>
        Age:
        <br />
        <input type="text" name="txtAge" size="3" maxlength="3" onchange="textAge_onblur()" />
        </p>
        <p>
         My interest is:
        <p>Sports
        <input type="radio" name="sports"  value="sports"/>
        </p>
        <p>Politics
        <input type="radio" name="politics" value="politics" />
        </p>
        <p>Magazines
        <input type="radio" name="magazines" value="magazines">
        </p>
         
        <p>
        <input type="button" value="Submit Registration" name="btnCheckForm"  onclick="btnCheckForm_onclick()" >
        <input type = "button" value = "Clear Details" name="btnClear" onclick="btnClear_onclick()">
        </p>
  
  <div id="interests"></div>

2 Comments

Either way "works" - an array is probably more "elegant' solution
@JoshKerbel An array makes sense in cases whereby you can benefit from how Array.join() works; e.g. insert a newline between every element but not at the start or end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.