Results 1 to 7 of 7

Thread: how to adding new div / content onclick button?

  1. #1
    Join Date
    Jul 2009
    Location
    Binus University
    Posts
    472
    Thanks
    78
    Thanked 21 Times in 21 Posts

    Default how to adding new div / content onclick button?

    Hi, how to adding a new div / content onclick button?
    This is the idea:


    So if i click on the add more email, another textbox will added.
    i don't think this function is hide or show, so are there any advice for my problem here.

    Thank you..
    Last edited by davelf; 10-27-2011 at 01:37 AM. Reason: All script resolved by John
    _____________________

    David Demetrius // davejob
    _____________________

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    In coding often a picture is worth less than the actual code. And by mentioning hide and show you're implying that you're using jQuery. I seem to recall that you do in general, so I'll proceed on that basis.

    You can clone the input and append the clone to a containing div:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
    <script type="text/javascript">
    jQuery(function($){
    	var $email = $('#email1'), count = 1;
    	$('#addemail').click(function(e){
    		e.preventDefault();
    		var idname = 'email' + (++count);
    		$email.parent().append($email.clone().attr({id: idname, name: idname}));
    	});
    	
    });
    </script>
    <style type="text/css">
    #emails input {
    	display: block;
    }
    </style>
    </head>
    <body>
    <div id="emails">
    <input type="text" id="email1" name="email1">
    </div>
    <a id="addemail" href="#">Add Email Field</a>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    davelf (10-26-2011)

  4. #3
    Join Date
    Jul 2009
    Location
    Binus University
    Posts
    472
    Thanks
    78
    Thanked 21 Times in 21 Posts

    Default

    thanks John,
    it work.

    Here's the online version:
    http://metablast.net/hsco/send-to-friend.html
    _____________________

    David Demetrius // davejob
    _____________________

  5. #4
    Join Date
    Jul 2009
    Location
    Binus University
    Posts
    472
    Thanks
    78
    Thanked 21 Times in 21 Posts

    Default

    hmm, John have another problem here.
    How to limit the user adding div??
    _____________________

    David Demetrius // davejob
    _____________________

  6. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    In one sense it's easy, but in another sense, you cannot. To be spammer proof that would have to be done on the server side. Spammers could copy your form and alter the javascript, or just put in as many hard coded email fields as they like.

    But in the design sense you can limit the number right here:

    Code:
    jQuery(function($){
    	var $email = $('#email1'), count = 1;
    	$('#addemail').click(function(e){
    		e.preventDefault();
    		if(count > 5){
    			alert("That's enough already!");
    			return;
    		}
    		var idname = 'email' + (++count);
    		$email.parent().append($email.clone().attr({id: idname, name: idname}));
    	});
    	
    });
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. The Following User Says Thank You to jscheuer1 For This Useful Post:

    davelf (10-27-2011)

  8. #6
    Join Date
    Sep 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to specify where a cloned div appears

    Quote Originally Posted by jscheuer1 View Post
    In coding often a picture is worth less than the actual code. And by mentioning hide and show you're implying that you're using jQuery. I seem to recall that you do in general, so I'll proceed on that basis.

    You can clone the input and append the clone to a containing div:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
    <script type="text/javascript">
    jQuery(function($){
    	var $email = $('#email1'), count = 1;
    	$('#addemail').click(function(e){
    		e.preventDefault();
    		var idname = 'email' + (++count);
    		$email.parent().append($email.clone().attr({id: idname, name: idname}));
    	});
    	
    });
    </script>
    <style type="text/css">
    #emails input {
    	display: block;
    }
    </style>
    </head>
    <body>
    <div id="emails">
    <input type="text" id="email1" name="email1">
    </div>
    <a id="addemail" href="#">Add Email Field</a>
    </body>
    </html>
    John, thanks so much for this code, it came in really handy for an assignment I'm doing. In my case, I'm cloning a div located in the middle of a long HTML form. I want the child div to appear directly under the parent div in the form. Using the code here, the child div appears at the end of the form, after the submit button. How do I specify where in the form I want the child element to appear?

  9. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You are allowed to have div or span elements inside a form, so for your form you can do something like so:

    HTML Code:
    <form action="#">
    <!-- other form stuff if any can go here -->
    <div> <!-- this can be a span if you prefer -->
    <input type="text" id="email0" name="email0">
    </div>
    <a id="addemail" href="#">Add Email</a>
    <!-- other form stuff if any can go here -->
    <input type="submit" value="Submit">
    </form>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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