-3

I'm having a lot of difficulty with my coding, especially with trying to find where it's going wrong, as I keep getting a 'func1 is not defined'

<html>

<body>

  <div id="gpa_calc" class="gpa_calc_overview" role="main" style="
margin-top:20px;border-bottom: 3px dashed black;">
    <div id="d_content_inner" class="d2l-page-main-padding" style="margin-bottom: 20px;">
      <div id="d_page_header" class="d2l-page-header">
        <div class="d2l-tool-actions d2l-left">
          <div class="d2l-heading">
            <h1 id="d_page_title" class="d2l-heading-title">
              <span class="vui-heading-1">GPA Calculator</span>
            </h1>
          </div>
        </div>
        <span class="d2l-clear"></span>
      </div>
      <div id="z_h" class="d_ma">
      </div>
      <div id="d_content_r_c1">
        <div id="d_content_r_c2">
          <div id="d_content_r">
            <div id="d_content_r_p">
              <form method="post" action="main.d2l?ou=148001" id="d2l_form" enctype="multipart/form-data" onreset="return false;">
                <div style="margin-top: 20px;">
                  <input type="hidden" name="d2l_action" value="">
                  <input type="hidden" name="d2l_actionparam" value="">
                  <input type="hidden" name="d2l_hitCode" value="547235486276855661">
                  <input type="hidden" name="d2l_rf" value="">
                  <input type="hidden" name="d2l_controlMapPrev" value="">
                  <span style="display:none;" id="z_a_values"></span>
                  <div class="clear">
                  </div>
                  <table id="gpa-calc-table" class="d_g d_gl" summary="List of grade items and their values">
                    <tbody>
                      <tr style="visibility:hidden;" class="fgskip">
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                      </tr>
                      <tr class="gpa_calc_headings">
                        <th scope="col" class="d_hch d_gl">Course Name</th>
                        <th scope="col" class="d_hch d_gr" style="padding-left:0.1em;">Class Grade</th>
                        <th scope="col" class="d_hch d_gr" style="padding-left:0.1em;">Grade Points</th>
                        <th scope="col" class="d_hch d_gr" style="padding-left:0.1em;">Credit Units</th>
                        <th scope="col" class="d_hch d_gr" style="padding-left:0.1em;">Total Grade Points</th>
                      </tr>
                      <tr class=" d_firstRow">

                        <th scope="row" class="d_gt d_ich">
                          <label class="subject1" contenteditable=""><strong>First Class</strong></label>
                        </th>
                        <td class="d_gn d_gr d_gt">
                          <label id="r1_cg" contenteditable="">76.8</label>
                        </td>

                        <td class="d_gn d_gr d_gt">
                          <label id="r1_gp" contenteditable=""></label>
                        </td>
                        <td class="d_gn d_gr d_gt">
                          <label id="r1_cu" contenteditable="">0-5</label>
                        </td>
                        <td class="d_gn d_gr d_gt">
                          <label id="r1_tgp" contenteditable="">CU * GP</label>
                        </td>

                      </tr>
                      <tr class=" d_secondRow">

                        <th scope="row" class="d_gt d_ich">
                          <label class="subject2" contenteditable=""><strong>Second Class</strong></label>
                        </th>
                        <td class="d_gn d_gr d_gt">
                          <label id="r2_cg" contenteditable="">0-100%</label>
                        </td>

                        <td class="d_gn d_gr d_gt">
                          <label id="r2_gp" contenteditable="">0-4.2</label>
                        </td>
                        <td class="d_gn d_gr d_gt">
                          <label id="r2_cu" contenteditable="">0-5</label>
                        </td>
                        <td class="d_gn d_gr d_gt">
                          <label id="r2_tgp" contenteditable="">CU * GP</label>
                        </td>

                      </tr>

                      <tr class=" d_thirdRow">

                        <th scope="row" class="d_gt d_ich">
                          <label class="subject3" contenteditable=""><strong>Third Class</strong></label>
                        </th>
                        <td class="d_gn d_gr d_gt">
                          <label id="r3_cg" contenteditable="">0-100%</label>
                        </td>

                        <td class="d_gn d_gr d_gt">
                          <label id="r3_gp" contenteditable="">0-4.2</label>
                        </td>
                        <td class="d_gn d_gr d_gt">
                          <label id="r3_cu" contenteditable="">0-5</label>
                        </td>
                        <td class="d_gn d_gr d_gt">
                          <label id="r3_tgp" contenteditable="">CU * GP</label>
                        </td>

                      </tr>
                      <tr class=" d_fourthRow">

                        <th scope="row" class="d_gt d_ich">
                          <label class="subject4" contenteditable=""><strong>Fourth Class</strong></label>
                        </th>
                        <td class="d_gn d_gr d_gt">
                          <label id="r4_cg" contenteditable="">0-100%</label>
                        </td>

                        <td class="d_gn d_gr d_gt">
                          <label id="r4_gp" contenteditable="">0-4.2</label>
                        </td>
                        <td class="d_gn d_gr d_gt">
                          <label id="r4_cu" contenteditable="">0-5</label>
                        </td>
                        <td class="d_gn d_gr d_gt">
                          <label id="r4_tgp" contenteditable="">CU * GP</label>
                        </td>

                      </tr>
                      <tr class=" d_fifthRow">

                        <th scope="row" class="d_gt d_ich">
                          <label class="subject5" contenteditable=""><strong>Fifth Class</strong></label>
                        </th>
                        <td class="d_gn d_gr d_gt">
                          <label id="r5_cg" contenteditable="">0-100%</label>
                        </td>

                        <td class="d_gn d_gr d_gt">
                          <label id="r5_gp" contenteditable="">0-4.2</label>
                        </td>
                        <td class="d_gn d_gr d_gt">
                          <label id="r5_cu" contenteditable="">0-5</label>
                        </td>
                        <td class="d_gn d_gr d_gt">
                          <label id="r5_tgp" contenteditable="">CU * GP</label>
                        </td>

                      </tr>
                      <tr class=" d_sixthRow">

                        <th scope="row" class="d_gt d_ich">
                          <label class="subject6" contenteditable=""><b>Random Class 6</b></label>
                        </th>

                        <td class="d_gn d_gr d_gt">
                          <label id="r6_cg" contenteditable="">0-100%</label>
                        </td>
                        <td class="d_gn d_gr d_gt">
                          <label id="r6_gp" contenteditable="">0-4.2</label>
                        </td>
                        <td class="d_gn d_gr d_gt">
                          <label id="r6_cu" contenteditable="">0-5</label>
                        </td>
                        <td class="d_gn d_gr d_gt">
                          <label id="r6_tgp" contenteditable="">CU * GP</label>
                        </td>
                      </tr>

                    </tbody>
                  </table>
                  <input type="hidden" name="d2l_controlMap" value="[{'grid_gradeitems':['z_a','Grid',[2,0,0,0,1,1,'',0,1,{},{},['h1','i00100100001110_i1_272994_00000000',['Incident Reporting','']],1,'','GradeItems'],{},0,1],'ph_readonly':['z_b','Container',[0,0,0,1,1,1,[1,'#999999',1],0,0,0,0,''],{},0,1],'dg_showgrade':['z_c','DisplayGroup',[1],{},0,1],'lbl_view':['z_d','Label',[],{},0,1],'ctl_messagearea':['z_h','MessageArea',['d_content_inner','d_page_header',0,[],null,null],{},0,0]},{'ph_readonly':{'ctl_1':['z_e','Container',[0,0,0,1,1,1,[1,'#999999',1],0,0,0,0,''],{},0,1]},'dg_showgrade':{'ctl_2':['z_f','DisplayGroup',[1],{},0,1]},'lbl_view':{'ctl_3':['z_g','Label',[],{},0,1]}]">
                  <input type="hidden" name="d2l_state" value="[{'3':['grid','pagesize','htmleditor','hpg'],'1':['gridpagenum','search','pagenum'],'2':['lcs']},[]]">
                  <input type="hidden" name="d2l_referrer" value="fyl6r08rUFiZjDTgmNas0UFWD3OZHEOU">
                </div>
              </form>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div>
      <p style="
text-align: center; text-decoration:underline; "><a href="https://www.fanshawec.ca/programs-and-courses" target="_blank" style="color:black;"><b> Link to Find the Appropriate Credits for Each Course</b></a> </p>
      <p style="text-align:center;color:black;"><b>Just Scroll Down to Find Your Course
<b></b></b>
      </p>
    </div>
  </div>
  <button onclick="func1()">Calculate</button>
  <script>
    function func1() {
      var x = document.getElementById('r1_cg').innerText;
      var out = document.getElementById('r1_gp');

      var a = 49.9;
      var b = 54.9;
      var c = 59.9;
      var d = 64.9;
      var e = 69.9;
      var f = 74.9;
      var g = 79.9;
      var h = 89.9;
      if (x > h) {
        out.innerText = 4.2
      } else if (x > g) {
        out.innerText = 4.0
      } else if (x > f) {
        out.innerText = 3.5
      } else if (x > e) {
        out.innerText = 3.0
      } else if (x > d) {
        out.innerText = 2.5
      } else if (x > c) {
        out.innerText = 2.0
      } else if (x > b) {
        out.innerText = 1.5
      } else if (x > a) {
        out.innerText = 1.0
      }
    }
  </script>

</body>

</html>

In the snippet below, the label with id 'r1_gp' is supposed to change, dependant on what the value is in label 'r1_cg', (in this case 76.8) yet whatever I've tried, I always get back 'func1 is not defined'

Help?!

Toreono
  • 17
  • 6
  • 2
    Welcome to Stack Overflow! Please take the [tour], have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Your snippet doesn't demonstrate the error you're asking about (it does demonstrate a *different* error, specifically [this one](http://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element)). Please see also the "minimal" part of: [mcve] – T.J. Crowder Oct 13 '17 at 12:26
  • What is the different error? And the div provided is all that's related to the task I'm trying to accomplish – Toreono Oct 13 '17 at 12:28
  • 1
    `var x = document.getElementById('r1_cg').innerText;` I can't see any element with id `r1_cg`.. You have a `r1cg`, changing this to `r1_cg` makes you snippet run without any errors. – Keith Oct 13 '17 at 12:28
  • 2
    @Toreono: *"What is the different error?"* Click "Run Code Snippet" and then click "Calculate", and see the "this one" link in my comment (I added it a few seconds after originally posting). And no, all of that code and markup is *not* necessary to demonstrate the problem, hence my comment about "minimal" -- please do read through [mcve] to understand why that matters and how it's useful to you. – T.J. Crowder Oct 13 '17 at 12:29
  • Where would I put the window.func1=func1 in? Where I had the button? Also, side note yet more confusing. I input the entire HTML when I went to change/edit the post, too lengthy, but running the snippet worked. The outcome I had wanted worked, but on the actually webpage it does not work. Lots of confusion – Toreono Oct 13 '17 at 12:39
  • One thing missing from your HTML is the doctype,. In IE etc, doctype's could effect quirks mode etc. so maybe on your website put ` ` at the top of your HTML. The snippets on SO will be doing this implicitly.. – Keith Oct 13 '17 at 12:43
  • Website has the The website is premade, I'm just working with the 'inspect element'. Meeting with the site owners/editors on wednesday for this – Toreono Oct 13 '17 at 12:44
  • With snippets not showing the error, are you able to expose the URL were this is happening.? – Keith Oct 13 '17 at 12:46
  • It's a website for students at my college. My goal for the project is to collaborate separate pages of grades onto one using iframes. The iframe portion is working properly, I'm just working on a GPA calculator, and the r1_gp id is the grade points acquired, based off of r1_cg id of the students' class grade – Toreono Oct 13 '17 at 12:49
  • Unfortunately without seeing the error, it's going to be hard for anyone to help. You say your using IFrame, I assume your Scripts are running in the same IFrame, otherwise `func1` would be undefined. – Keith Oct 13 '17 at 12:52

1 Answers1

0

In your code "id" of the "label" is not "r1_cg", it is "r1cg". You can try modify "id" of "label" or in your "function" change: "document.getElementById('r1_cg').innerText;" to "document.getElementById('r1cg').innerText;"