www.webdeveloper.com
Results 1 to 10 of 10

Thread: [RESOLVED] adding html controls Value problem

  1. #1
    Join Date
    Nov 2009
    Posts
    3

    resolved [RESOLVED] adding html controls Value problem

    Hello,

    i want to use the adding html controls found on the site. No is it working okay except one thing. I added another textfield. So now when i press the button there will be two textfields: Name and Email. So far so good. Now, when i fill in the two textfields and add another field by pressing the button the two fields get the same value. I coppied the text below.

    As you can see i am bad at english and even worse with JS but i really want to use this in my script

    function createInput(id,value) {
    return "Naam: <input name='naam[]' type='text' id='naam "+ id +"' onChange='javascript:saveValue("+ id +",this.value)' value='"+ value +"'> Email: <input name='email[]' type='text' id='email "+ id +"' onChange='javascript:saveValue("+ id +",this.value)' value='"+ value +"'><br>";
    }

    i hope you can help me

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    How is this inserted into the document?
    id must be unique in the document.
    Using correct dom methods would be better:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <title>add remove input</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    <script type="text/javascript">
    function addInput() {
    var parent=document.getElementsByTagName('fieldset')[0];
    var aInput=parent.getElementsByTagName('input');
    var o;
    var oName=aInput[0].name+aInput.length
    try { //IE method
    	o=document.createElement('<input type="text" name="'+oName+'">');
    }
    catch (error) { // DOM method
        var o=document.createElement("input");
        o.setAttribute('type', 'text');
        o.setAttribute('name', oName);
    }
    parent.appendChild(o);
    }
    
    function removeInput() {
    var parent=document.getElementsByTagName('fieldset')[0];
    var aInput=parent.getElementsByTagName('input');
    if(aInput.length>1) { // leave a least 1 input
        parent.removeChild(aInput[aInput.length-1]);
        }
    }
    </script>
    
    <style type="text/css">
    input {display:block;} 
    </style>
    
    </head>
    <body>
    <form action="#" method="post" name="form1">
    <fieldset><legend>list</legend>
    	<input type="text" name="foo">
    </fieldset>
    <div>
        <button type="button" onclick="addInput()">addInput</button>
        <button type="button" onclick="removeInput()">removeInput</button>
        <button type="submit" name="list">submit</button>
    </div>
    </form>
    </body>
    </html>
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    Nov 2009
    Posts
    3

    i am not even a js dummie

    Hey thanks for your reaction,

    I have tried your code but now i only get one textfield. That was working with the other as well. But when i want two textfield i get the problem of the wrong value in the wrong field after pressing "add new". I will use it in PHP so i used the [] after the name so i can count() the post results.

    But my problem is i cant write javascript i try to read and understand it but when i change something it aint working anymore

    so maybe someone can explain me how i can get:
    Name: <textfield> Email: <textfield>
    After pressing the add button and the value is not changing when i add another one.

    I really hope someone understand my messedup mind

  4. #4
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <title>clone</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    <script type="text/javascript">
    function clone() {
    var parent=document.getElementById('naw0');
    var cloned=parent.cloneNode(true);
    // unique id
    var aNaam=document.getElementsByName('naam[]');
    var num=aNaam.length;
    cloned.id='naw'+num;
    //insert after last pair
    parent.parentNode.insertBefore(cloned, document.getElementById('naw'+(num-1)).nextSibling);
    //clear inputs
    var aInput=cloned.getElementsByTagName('input');
    aInput[0].value=aInput[1].value='';
    }
    </script>
    
    </head>
    <body>
    <form action="#" method="post" name="form1">
    <div id="naw0">
    <label>Naam:<input type="text" name="naam[]"></label>
    <label>Email:<input type="text" name="email[]"></label>
    </div>
    <button type="submit" name="submit" value="done">submit</button>
    <button type="button" onclick="clone();">clone</button>
    </form>
    </body>
    </html>
    At least 98% of internet users' DNA is identical to that of chimpanzees

  5. #5
    Join Date
    Nov 2009
    Posts
    3
    That is what i want! GREAT! THANKS!

  6. #6
    Join Date
    Nov 2009
    Location
    Netherlands
    Posts
    21
    I'm also using this script but when i clone my input field it doesn't clear it....

    This is my code:

    Javascript:
    Code:
        <script type="text/javascript">
    		function clone() {
    		var parent=document.getElementById('veld0');
    		var cloned=parent.cloneNode(true);
    		// unique id
    		var aNaam=document.getElementsByName('apparatuur[]');
    		var num=aNaam.length;
    		cloned.id='veld'+num;
    		//insert after last pair
    		parent.parentNode.insertBefore(cloned, document.getElementById('veld'+(num-1)).nextSibling);
    		//clear inputs
    		var aInput=cloned.getElementsByTagName('input');
    		aInput[0].value=aInput[1].value='';
    	}
    </script>
    Form:
    Code:
    <form name="form1" action="sql.php" method="POST">
        		<div id="veld0">
        <label>
          		<span class="tekst">Apparaatnaam:</span>
         	<input type="text" name="apparatuur[]" size="25" maxlength="25" />
        </label>
        <br />
    </div>
        	<p>
          <input type="submit" name="voegtoe" value="Voeg toe" />
          <input type="button" onclick="clone();" value="extra invoerveld" />
    </form>

  7. #7
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    var aInput=cloned.parentNode.getElementsByTagName('input');
    At least 98% of internet users' DNA is identical to that of chimpanzees

  8. #8
    Join Date
    Nov 2009
    Location
    Netherlands
    Posts
    21
    Thanks, but when I use this it also clears the first field.... Is there any possibility that it only adds a clear field without clearing other fields(A)

  9. #9
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    //clear input
    var aInput=cloned.parentNode.getElementsByTagName('input');
    aInput[aInput.length-3].value='';
    // or
    var aInput=cloned.getElementsByTagName('input');
    aInput[0].value='';
    Last edited by Fang; 11-23-2009 at 07:00 AM.
    At least 98% of internet users' DNA is identical to that of chimpanzees

  10. #10
    Join Date
    Nov 2009
    Location
    Netherlands
    Posts
    21
    Thanks a lot! Everything works great now!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles