Link to home
Start Free TrialLog in
Avatar of Saqib Khan
Saqib KhanFlag for United States of America

asked on

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.
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Because the attribute is "id", not "ID"

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



Avatar of Saqib Khan

ASKER

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
            }
      }
 
 }

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
SOLUTION
Avatar of Pravin Asar
Pravin Asar
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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>
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;
 
}
>> 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;
}
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:)
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
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>
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")
            }

      }
it always returns "GRRRRRRR". i know i am missing "}" for closing function above:)
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
            }
            
      }
 
 }
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>
I saw your post at Date: 07/24/2006 06:41PM EDT

This one looks okay to me.

Good Bye.

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
You are welcome.