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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    javascript newbie in need of help with my function

    so I am working on my company intranet site and made a function:
    <script language="javascript">
    <!-- Begin
    var j;
    var a="designer";
    var b="accountant";
    var c="consultant";
    function description(j) {
    if (j > 0){
    form.job.value = a;
    }
    if (j > 1){
    form.job.value = b;
    }
    if (j > 2){
    form.job.value = c;
    }
    }
    End -->
    </script>


    I already know since this is my first function, there are major issues with it, what I want to do is a form with checkboxes is on this webpage I made, when you select the textboxes, I want to pass the value to the above function, and have the function export to a hidden form called "job" which will be saved out to a file which already works fine, but the output is blank which leads me to believe either the function is wrong, or the value from the checkboxes are not passing to the function or it's not outputting to the hidden field "job" as it's value...
    here is the code for the check boxes:
    <input type="checkbox" name="job1" value="1"><b>CAD
    design/drafting</b>
    <p><input type="checkbox" name="job2" value="2"><b>Accounting</b></p>
    <p><input type="checkbox" name="job3" value="3"><b>Consulting</b></p>


    and here is the code for the hidden field:
    <input type=hidden name=job onFocus="this.blur()">

    and lastly, here is the code which should pass the value of the checkboxes upon clicking submit (ignore the other functions that it submits through, they work just great...):
    <input type="submit" value="Submit" name="SubmitNetworkRecord" tabindex="100" onFocus="changeCase(this.form.firstname); changeCase(this.form.lastname)" onClick="javascript:makeUserID(this.form, this.form.firstname.value,
    this.form.lastname.value); description(this.form.job1.value); description(this.form.job2.value); description(this.form.job3.value)">

  • #2
    Regular Coder
    Join Date
    May 2004
    Location
    Alabama, USA
    Posts
    237
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Michael, Welcome to Coding Forums!

    A few suggestions:

    1. IMO, using html comments in script elements is not needed any more. Also note that that line is not a Js comment so the "Begin" and "End" comments you have there are going to cause Js errors.

    2. I suggest not using "form" as the "name" of a form.

    3. I suggest a more standard way of accessing form element objects. For example if the form has name="form1" then access it like this:
    Code:
    var f = document.forms['form1'];
    if the form has id='form1' then access it like this:
    Code:
    var f = document.getElementById('form1');

  • #3
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you for your suggestions, but I am still very lost/confused...so are the variables from those check boxes being passed to the function? or is it failing afterwards, such as passing the output from the function to the value of the hidden form field..?

  • #4
    Regular Coder
    Join Date
    May 2004
    Location
    Alabama, USA
    Posts
    237
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I saw several things I wanted to point out - but I'm headed out the door and it was quicker to just type the following. I may not have chosen the best ways of doing things. Compare it to your code and ask any questions you want.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Template</title>
    <script type='text/javascript'>
    window.onload = function()
    {
      var f = document.forms['form1'];
      f.onsubmit = form1OnSubmit;
      f.SubmitNetworkRecord.onfocus = snrOnFocus;
    }
    var jobs = {"1":"designer", "2":"accountant", "3":"consultant"};
    function description(j) {
      var desc = jobs[j];
      return desc || "invalid job number";
    }
    function form1OnSubmit()
    {
    //  makeUserID(this, this.firstname.value, this.lastname.value);
      var j;
      if (this.job1.checked) {
        j = description(this.job1.value);
        alert(j);///////
      }
      if (this.job2.checked) {
        j = description(this.job2.value);
        alert(j);///////
      }
      if (this.job3.checked) {
        j = description(this.job3.value);
        alert(j);///////
      }
      this.job.value = j;
    //  return false;
    }
    function snrOnFocus()
    {
    //  changeCase(this.form.firstname);
    //  changeCase(this.form.lastname)"
    }
    </script>
    </head>
    <body>
    
    <form name='form1'>
    <input type="checkbox" name="job1" value="1"><b>CAD design/drafting</b>
    <p><input type="checkbox" name="job2" value="2"><b>Accounting</b></p>
    <p><input type="checkbox" name="job3" value="3"><b>Consulting</b></p>
    <input type=hidden name=job onFocus="this.blur()">
    <input type="submit" value="Submit" name="SubmitNetworkRecord" tabindex="100">
    </form>
    
    </body>
    </html>

  • #5
    New Coder
    Join Date
    Jan 2007
    Posts
    93
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if (j > 0){form.job.value = a}
    if (j > 1){form.job.value = b}
    if (j > 2){form.job.value = c}
    If J=3 All the 'if' sentances will be true and
    it will do them all, should still be OK as the is will stop on the last that was true.

    But that should still be avoided.

  • #6
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    mikefoster, you are a god! thank you ever so much...works perfectly!

    -Michael

  • #7
    Regular Coder
    Join Date
    May 2004
    Location
    Alabama, USA
    Posts
    237
    Thanks
    0
    Thanked 0 Times in 0 Posts


  •  

    Posting Permissions

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