Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using arrays to manipulate results of check boxes in a form.

    Hello, I am very new to javascript and coding in general. I had a few programming courses 10 years ago in engineering school but never stuck with it.

    My larger goal is to create a website with 20 check boxes which the user can select from. When they hit the submit button, the java script will read which boxes are selected do a probability calculation (each of the 20 options will multiply a variable by a different percent and the end result will be displayed.

    As I said my coding is limited and by looking at other code on websites I liked and reading a few tutorials I have came up with a working html form and JS that uses the following two functions to read and display my results.

    Code:
    <SCRIPT LANGUAGE="JavaScript">
             function getSelected(opt) {
                var selected = new Array();
                var index = 0;
                for (var intLoop = 0; intLoop < opt.length; intLoop++) {
                   if ((opt[intLoop].selected) ||
                       (opt[intLoop].checked)) {
                      index = selected.length;
                      selected[index] = new Object;
                      selected[index].value = opt[intLoop].value;
                      selected[index].index = intLoop;
                   }
                }
                return selected;
             }
    
             function outputSelected(opt) {
                var sel = getSelected(opt);
                var strSel = "";
                for (var item in sel)       
                   strSel += sel[item].value + "\n";
                alert("Selected Items:\n" + strSel);
             }
          </SCRIPT>
    Here is an example of one of the form checkbox elements

    <p><input type="checkbox" name="chkboxarray" value="C" id="C"> : <label for="cb2">C<br></label></p><TD>

    the function is called from the form button as

    <INPUT TYPE=BUTTON VALUE="Submit"
    ONCLICK="outputSelected(this.form.chkboxarray);">

    Now that I have given you the background information. I am trying to read the results of the form and do the probability calculation. My basic logic is
    if ? is checked then multiply prob by x
    I was thinking of doing this for all 20 values.

    Again I am very new to this, but I think this should work

    if (selected.indexOf(‘C’) != -1) {
    prob = prob * 0.20 }


    but the debugger keeps telling me that 'selected" is undefined.

    I can't find the issue I am new to arrays . Is there a better more efficient way?

    Thanks


    Jeremy


    P.S. If it is helpful I can post the entire code including the HTML, just let me know.

  • #2
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    Post the code that is returning your error.
    Software and cathedrals are much the same - first we build them, then we pray.

  • #3
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for the reply

    Code:
     function probability() {
     var prob = 1;
         if (selected.indexOf(C) != -1) {prob = prob * 0.20 } ;
             alert(+ prob)}

    This is the problem function it returns "selected is undefined'

    selected is defined in the getSelected function
    Do I need to bury this code into getSelected?


    Again, thanks for the reply

    Jeremy

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,506
    Thanks
    77
    Thanked 4,378 Times in 4,343 Posts
    A variable declared inside a function is *ONLY* available within that function.

    To make it available to other code, move the declaration outside of any function.

    ALSO: The indexOf method is only applicable to strings. Your selected variable is not a string. See below.


    Code:
    <script type="text/javascrip"> <!-- language= is obsolete -->
        var selected = null; // delaration is now page scope
    
        function getSelected(opt) {
                selected = new Array(); // **OMIT** the var keyword here!
                var index = 0;
                ...
       }
    
       ...
       function probability() {
           if ( selected == null ) 
           {
               alert("you must push the submit button first");
               return;
           }
           var prob = 1;
           if (selected[C] != -1) {prob = prob * 0.20 } ;
           ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Old Pendant


    I will tinker with this awhile and let you guys know how it goes.

    Much appreciate the time and guidance.



    Jeremy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •