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 08: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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.15761 seconds
  • Memory Usage 2,947KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (6)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (10)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates