www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 11-17-2009, 04:31 PM
    Justtoby Justtoby is offline
    Registered User
     
    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
    Reply With Quote
      #2  
    Old 11-18-2009, 02:57 AM
    Fang's Avatar
    Fang Fang is offline
    Resistance is futile
     
    Join Date: Apr 2003
    Location: Netherlands
    Posts: 18,435
    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
    Reply With Quote
      #3  
    Old 11-18-2009, 05:59 AM
    Justtoby Justtoby is offline
    Registered User
     
    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
    Reply With Quote
      #4  
    Old 11-18-2009, 07:13 AM
    Fang's Avatar
    Fang Fang is offline
    Resistance is futile
     
    Join Date: Apr 2003
    Location: Netherlands
    Posts: 18,435
    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
    Reply With Quote
      #5  
    Old 11-18-2009, 07:17 AM
    Justtoby Justtoby is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 3
    That is what i want! GREAT! THANKS!
    Reply With Quote
      #6  
    Old 11-23-2009, 06:47 AM
    mjstam1989 mjstam1989 is offline
    Registered User
     
    Join Date: Nov 2009
    Location: Netherlands
    Posts: 7
    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>
    Reply With Quote
      #7  
    Old 11-23-2009, 06:59 AM
    Fang's Avatar
    Fang Fang is offline
    Resistance is futile
     
    Join Date: Apr 2003
    Location: Netherlands
    Posts: 18,435
    Code:
    var aInput=cloned.parentNode.getElementsByTagName('input');
    __________________
    At least 98% of internet users' DNA is identical to that of chimpanzees
    Reply With Quote
      #8  
    Old 11-23-2009, 07:31 AM
    mjstam1989 mjstam1989 is offline
    Registered User
     
    Join Date: Nov 2009
    Location: Netherlands
    Posts: 7
    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)
    Reply With Quote
      #9  
    Old 11-23-2009, 07:57 AM
    Fang's Avatar
    Fang Fang is offline
    Resistance is futile
     
    Join Date: Apr 2003
    Location: Netherlands
    Posts: 18,435
    Code:
    //clear input
    var aInput=cloned.parentNode.getElementsByTagName('input');
    aInput[aInput.length-3].value='';
    // or
    var aInput=cloned.getElementsByTagName('input');
    aInput[0].value='';
    __________________
    At least 98% of internet users' DNA is identical to that of chimpanzees

    Last edited by Fang; 11-23-2009 at 08:00 AM.
    Reply With Quote
      #10  
    Old 11-23-2009, 08:38 AM
    mjstam1989 mjstam1989 is offline
    Registered User
     
    Join Date: Nov 2009
    Location: Netherlands
    Posts: 7
    Thanks a lot! Everything works great now!
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 02:40 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.