Betty Mock
Betty Mock

Reputation: 1393

javascript losing id when passing it to the function

In Coldfusion the program person1.cfm has code like this:

<form action = "person2.cfm"
  id       = "pers2form" 
  name     = "pers2form"
  method   = "post"      
  onsubmit = "return pers1()">
  <table>
      <td>
        <input type = "radio"  
          class = "moxradio round"                   
          id    = "Person"
          name  = "whereto"                                                        
          value = "Person">
      </td>
      <td style = "color: ##946ca6"> Update Person</td>
    <tr>
    <td>
      <input type = "radio"
                class = "moxradio round"
                id    = "Persactiv"
                name  = "whereto" 
                value = "PersActiv">
    </td>
    <td style = "color: ##946ca6; ">Update Activity  </td>
  </table>
</form>
<input type  = "Submit"
  name  = "subpers2"
  id    = "subpers2" 
  class = "submitbut"
  style = "display: block; margin:10px auto 10px auto"
  value = "Submit">

This is working fine, and submits properly to person2.cfm which has code like this:

<cfif IsDefined('form.whereto')>
   <cfset whereto = form.whereto> 
</cfif>

<cfinclude template = person1.cfm>
<cfoutput>
  <script>
   recheck('#whereto#')
   function recheck(target) {
      alert('target is ' + target);
      document.getElementById(target).checked = true;
   }
  </script>
</cfoutput>

This is supposed to put the check back in the radio box in person1. But it doesn't. I get this error on the console:
TypeError: document.getElementById(...) is null[Learn More] person2.cfm:393:1

The function recheck is picking up the right target. Since person1.cfm is already included, that target should be right there. But it can't seem to find it.

The rest of person2.cfm comes up correctly.

The reference to line 393 is spurious since there is no line 393. The null value must occur within the function recheck. I've done this kind of thing all over the place, putting values back into input fields and select boxes and it's all working. But I've never done it with a radio button. Maybe there is something different about rechecking a box? Or maybe I've done something stupid I just can't see?

Can anyone spot what I am doing wrong?

Upvotes: 0

Views: 56

Answers (1)

malpaso
malpaso

Reputation: 177

The id of the second radio button is 'Persactiv' however the value your sending is 'PersActiv'. Case-sensitivity ends up in getElementById() not finding the id.

Upvotes: 1

Related Questions