CreateElement Problem

I have ta basic questions, i am not in mode to do debug so please help me out here.


Question, textbox i created with javaScript it is not passing value to next page, so on the next page how can i get the value of it. i am using ASP Request object to access its value, but i believe when we create new elements using jscript it does not event post value as a form element.

 function signIt(obj) {

     if(obj.checked && obj.value=="agree_terms") {
          var div, txtLabel, txtbox
         
          div = document.createElement("DIV")
          div.setAttribute("id", "newDiv")
         
          txtLabel = document.createTextNode("Please sign your initials: ")
          txtbox = document.createElement("INPUT")
          txtbox.setAttribute("TYPE", "TEXTBOX")
          txtbox.setAttribute("NAME", "signBox")
         
          div.appendChild(txtLabel)
          div.appendChild(txtbox)
         
          document.getElementById("cSignature").appendChild(div)

     } else {
          alert(document.getElementById("cSignature").innerHTML)
          if(document.getElementById("newDiv")) {  // < Does NOT EXIST
               document.getElementById("cSignature").removeChild(document.getElementById("newDiv"))
          }
     }
 
 }

in asp what we is Request("signBox") and its suppose to show the value of this textbox, i am doing a trick to update the value with a hidden field and then submit the form. do you guys have any other better suggestions?

Thanks in advance.
LVL 23
Saqib KhanSenior DeveloperAsked:
Who is Participating?
 
ZvonkoConnect With a Mentor Systems architectCommented:
My I add a small note: that dynamic scripting in the background is error prone and unnecessary.
Create the html element in html source and set them to disabled=true in html code. Then are those disabled form elements also not transmitted at form submit time. Additionally you can make them invisible or display=none until the checkbox for signature is clicked.
0
 
ZvonkoSystems architectCommented:
Because the attribute is "id", not "ID"

0
 
ZvonkoSystems architectCommented:
Also you want type=text not type=textbox. Such type does not exist.
 txtbox.setAttribute("type", "TEXT");



0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Saqib KhanSenior DeveloperAuthor Commented:
Thanks Zvonko, i fixed the ID problem already, but now why this this not working, after i fixed the textbox, i can not alert the value of it.

 function signIt(obj) {

 
      if(obj.checked && obj.value=="agree_terms"  ) {
            var div, txtLabel, txtbox
            
            div = document.createElement("DIV")
            div.setAttribute("id", "newdiv")
            
            txtLabel = document.createTextNode("Please sign your initials: ")
            txtbox = document.createElement("INPUT")
            txtbox.setAttribute("type", "TEXT")
            txtbox.setAttribute("name", "signBox")
            txtbox.setAttribute("size", 15)
            txtbox.className =  "signcls"
            
            div.appendChild(txtLabel)
            div.appendChild(txtbox)
            
            if(!isCreated) {
                  document.getElementById("cSignature").appendChild(div)
                  isCreated = true
            }
      alert(document.StandardCheck.signBox.value)  // <  HERE............
      } else {
            
            if(document.getElementById("newdiv")) {
                  document.getElementById("cSignature").removeChild(document.getElementById("newdiv"))
                  isCreated = false
            }
      }
 
 }

0
 
Saqib KhanSenior DeveloperAuthor Commented:
i am using the following now..

 function signIt(obj) {

 
      if(obj.checked && obj.value=="agree_terms"  ) {
            var div, txtLabel, txtbox
            
            div = document.createElement("DIV")
            div.setAttribute("id", "newdiv")
            
            txtLabel = document.createTextNode("Please sign your initials: ")
            txtbox = document.createElement("INPUT")
            txtbox.setAttribute("TYPE", "TEXT")
            txtbox.setAttribute("NAME", "signBox")
            txtbox.setAttribute("size", "15")
            txtbox.className =  "signcls"
            
            div.appendChild(txtLabel)
            div.appendChild(txtbox)
            
            if(!isCreated) {
                  document.getElementById("cSignature").appendChild(div)
                  isCreated = true
            }
      
      } else {
            alert(document.getElementById("cSignature").innerHTML)
            alert(document.StandardCheck.signBox.value)
            if(document.getElementById("newdiv")) {
                  document.getElementById("cSignature").removeChild(document.getElementById("newdiv"))
                  isCreated = false
            }
      }
 
 }


Alert shows textbox with the name and value pairs fine, but the second alert in else statement does not show textbox value, is that because form is been generated outside of the function,?

And its so confusing, when to use lowser case and when to use upper case when it comes to setAttributes.

Thanks
0
 
Pravin AsarConnect With a Mentor Principal Systems EngineerCommented:
To create a TextField , look at
Use lower case attribute names.


function CreateFormTextField (name, defValue) {
   var formFld   = document.createElement ("input");
   formFld.setAttribute ('type', 'text');
   formFld.setAttribute ('id', name);
   formFld.value = defValue;
   formFld.name  = name;
   return formFld;
}
0
 
Pravin AsarPrincipal Systems EngineerCommented:
See if works for you...

<html>
<body>
<script language="javascript">
function signIt(obj) {

 
     if(obj.checked && obj.value=="agree_terms"  ) {
          var div, txtLabel, txtbox
         
          div = document.createElement("DIV")
          div.setAttribute("id", "newdiv")
         
          txtLabel = document.createTextNode("Please sign your initials: ")
          txtbox = document.createElement("input")
          txtbox.setAttribute("type", "text")
          txtbox.setAttribute("name", "signBox")
          txtbox.setAttribute("size", "15")
          txtbox.className =  "signcls";

          txtbox.value ="I agree";
         
          div.appendChild(txtLabel)
          div.appendChild(txtbox)
             isCreated = false;    
          if(!isCreated) {
               document.getElementById("cSignature").appendChild(div)
               isCreated = true;
          }
     
     } else {
          alert(document.getElementById("cSignature").innerHTML)
          alert(document.StandardCheck.signBox.value)
          if(document.getElementById("newdiv")) {
               document.getElementById("cSignature").removeChild(document.getElementById("newdiv"))
               isCreated = false
          }
     }
 
 }
</script>
<form name="StandardCheck">
<input type="checkbox" value="agree_terms" onclick="signIt(this);">Terms of Agreement
<div id="cSignature"></div>
</form>
<body>
</html>
0
 
Saqib KhanSenior DeveloperAuthor Commented:
excellence of Excuation @ pravinasar

Yes it worked, now please tell me WHY it does not like setAttribute to specifiy the "name" attribute, when i changed it to txtbox.name = "bluh", it worked. why whats the difference, is that a bug?

and can i reference that new generated field into another function that i am using for onSubmit event? because its giving me error.

function checkme() {

alert(document.StandardCheck.mysignature.value)  // < ERROR

  if (document.StandardCheck.agree[1].checked)  {
    alert("You must read and agree to the Terms and Conditions. Please select the “I AGREE to the Terms and Conditions of Sale” option on the check out page.")
      return false;
  }  else return true;
 
}
0
 
Pravin AsarPrincipal Systems EngineerCommented:
>> alert(document.StandardCheck.mysignature.value)

I am assuming the field you created is mysignature

Also check the field agree .

Is it checkbox ? How many checkboxes with this name ?

If only one checkbox field , then following should work.
Also you have string build error quote in a quote.

if (document.StandardCheck.agree.checked) {
      msg="You must read and agree to the Terms and Conditions.\n";
     msg += "Please select the  \“I AGREE to the Terms and Conditions of Sale\” option on the check out page.\n";
      alert(msg);
     return false;
}
0
 
Saqib KhanSenior DeveloperAuthor Commented:
i am using two radio options, here is the full function...

  var isCreated;
  isCreated = false
 
 function signIt(obj) {

 
      if(obj.checked && obj.value=="agree_terms"  ) {
            var div, txtLabel, txtbox
            
            div = document.createElement("DIV")
            div.setAttribute("id", "newdiv")
            
            txtLabel = document.createTextNode("Please initialize: ")
            txtbox = document.createElement("INPUT")
            txtbox.setAttribute("type", "TEXT")
            txtbox.setAttribute("size", "15")
            txtbox.name = "mysignature"
            txtbox.className =  "signcls"
            
            div.appendChild(txtLabel)
            div.appendChild(txtbox)
            
            if(!isCreated) {
                  document.getElementById("cSignature").appendChild(div)
                  isCreated = true
            }
      
      } else {

            if(document.getElementById("newdiv")) {
                  document.getElementById("cSignature").removeChild(document.getElementById("newdiv"))
                  document.StandardCheck.mysignature.value = ""  \\ < ERROR HERE **********
                  isCreated = false
            }
            
      }
 
 }


i am almost there, i am sure with your great help i can do this:)
0
 
Saqib KhanSenior DeveloperAuthor Commented:
another Interesting thing...

i am trying to do a alert on the INNER html of main DIV and if i change TYPE to type it does not show the type attribute in the dynmaicly generated HTML the name attribute does not even exist, but its funny in ASP i can grab the vale of this newly generated textbox.

Thanks
0
 
Pravin AsarPrincipal Systems EngineerCommented:
document.getElementById("cSignature").removeChild(document.getElementById("newdiv"))

will remove the element div and it children, hence a error.

The default form input element type is text. .. so it is okay.


try this ...

<form>
<input name="abc" value="xyz">
</form>
0
 
Saqib KhanSenior DeveloperAuthor Commented:
I am sorry, in bove line Error line was suppose to appear before the removeChildm it was just a typo.

please look below, how simple is that but still it assumes field is never created..

 function signIt(obj) {

 
      if(obj.checked && obj.value=="agree_terms"  ) {
            var div, txtLabel, txtbox
            
            div = document.createElement("DIV")
            div.setAttribute("id", "newdiv")
            
            txtLabel = document.createTextNode("Please initialize: ")
            txtbox = document.createElement("input")
            txtbox.type= "text"
            txtbox.size =  "15"
            txtbox.className =  "signcls"
            txtbox.name = "mysignature"
            
            div.appendChild(txtLabel)
            div.appendChild(txtbox)            
            
            if(!isCreated) {
                  document.getElementById("cSignature").appendChild(div)
                  isCreated = true
            }
            
            if(document.getElementById("newdiv")) {
                  if(document.StandardCheck.mysignature) {
                        alert("YES")
            } else {
                        alert("GRRRRRRRRR")
            }

      }
0
 
Saqib KhanSenior DeveloperAuthor Commented:
it always returns "GRRRRRRR". i know i am missing "}" for closing function above:)
0
 
Saqib KhanSenior DeveloperAuthor Commented:
i gueess, i am too tired, been posting wrong code.

here is the correct function with problem (always throw NO in alert).

 function signIt(obj) {

 
      if(obj.checked && obj.value=="agree_terms"  ) {
            var div, txtLabel, txtbox
            
            div = document.createElement("DIV")
            div.setAttribute("id", "newdiv")
            
            txtLabel = document.createTextNode("Please initialize: ")
            txtbox = document.createElement("input")
            txtbox.type= "text"
            txtbox.size =  "15"
            txtbox.className =  "signcls"
            txtbox.name = "mysignature"
            
            div.appendChild(txtLabel)
            div.appendChild(txtbox)            
            
            if(!isCreated) {
                  document.getElementById("cSignature").appendChild(div)
                  isCreated = true
            }
            
            if(document.StandardCheck.mysignature) {
                  alert("YES")
            } else {
                  alert("NO")
            }

      } else {
            
            if(document.getElementById("newdiv")) {
                  document.StandardCheck.mysignature.value = ""
                  document.getElementById("cSignature").removeChild(document.getElementById("newdiv"))                  
                  isCreated = false
            }
            
      }
 
 }
0
 
Pravin AsarPrincipal Systems EngineerCommented:
Based on your previous post, here is corrected code.

<html>
<body>
<script language="javascript">
isCreated = false;
function signIt(obj)
{

     if(obj.checked && obj.value=="agree_terms"  ) {
          var div, txtLabel, txtbox
         
          div = document.createElement("DIV")
          div.setAttribute("id", "newdiv")
         
          txtLabel = document.createTextNode("Please initialize: ")
          txtbox = document.createElement("input")
          txtbox.type= "text"
          txtbox.size =  "15"
          txtbox.className =  "signcls"
          txtbox.name = "mysignature"
         
          div.appendChild(txtLabel)
          div.appendChild(txtbox)          
         
          if(!isCreated) {
               document.getElementById("cSignature").appendChild(div)
               isCreated = true
          }
         
          if(document.getElementById("newdiv")) {
               if(document.StandardCheck.mysignature) {
                    alert("YES");
                   }
               else {
                    alert("GRRRRRRRRR");
               }
            }
      }
}

</script>
<form name="StandardCheck">
<input type="checkbox" value="agree_terms" onclick="signIt(this);">Terms of Agreement
<div id="cSignature"></div>
</form>
<body>
</html>
0
 
Pravin AsarPrincipal Systems EngineerCommented:
I saw your post at Date: 07/24/2006 06:41PM EDT

This one looks okay to me.

Good Bye.

0
 
Saqib KhanSenior DeveloperAuthor Commented:
I think Zvonko is right, no matter what i do it works with one browser and then stops working with another. i added a onkeyup function to it and updating a the value of a hidden field with the newly created element (in order to make it cross-browser).
but sometimes we do need this feature like Adding new Table Rows, Multiple Form fields for file upload, simply display/hide will not work in that case because they count on user input.

Thanks Zvonko and pravinasar
0
 
ZvonkoSystems architectCommented:
You are welcome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.