0

I wanne add multiple users in jQuery. I have the following code but it doesnt work. Does someone knows why?

Thanks!

    <script type="text/javascript">

$( document ).ready(function() {
  $('#add').click(function(){
        var newuser = $.html('Gebruiker: <input type="text" name="user[]"><br />');

        $(newuser).appendTo('#users');
    });
});


</script>
<section>
<span id="add">Nieuwe gebruiker</span><br /><br /><br />
<form action="" method="" id="users">

<input type="submit" name="send" value="verzenden">
</form>
2
  • The new input field wont show up. I tried different codes but none of them work. With my current code i receive: Uncaught TypeError: Object function (e,t){return new b.fn.init(e,t,r)} has no method 'html' I know that $.html() doesnt work, but none of my tries helped Commented Feb 4, 2014 at 6:50
  • is there something $.html()?? Commented Feb 4, 2014 at 7:00

3 Answers 3

1

Use simply string in your variable, no jQuery .html() function required. Then change your .appendTo() function to .append() and define the element in jQuery selector.

$( document ).ready(function() {
    $('#add').click(function(){
        var newuser = 'Gebruiker: <input type="text" name="user[]"><br />';
        $('#users').append(newuser);
    });
});
Sign up to request clarification or add additional context in comments.

5 Comments

Doesnt work. Getting this error: Uncaught Error: Syntax error, unrecognized expression: Gebruiker: <input type="text" name="user[]"><br />
@DonnyvanV please provide a fiddle
Now i want to remove the element. My new code is this: jsfiddle.net/x52Cf/1 It doesnt even get clicked because no log will show up
You're running into dynamically created elements problems. For those, you need to use .on rather than .click. JsFiddle: jsfiddle.net/x52Cf/2
Use $(document).on('click','.delete', function(){ $(this).remove(); console.log('reached'); }); ... However you need to wrap all in a div to select all containing elements... I have updated my answer below...
1

In case you are wondering why your current code is not working:

string starting with anything other than "<" is not considered HTML string in jQuery 1.9

http://stage.jquery.com/upgrade-guide/1.9/#jquery-htmlstring-versus-jquery-selectorstring

Therefore this code will work fine (as it starts with <):

$( document ).ready(function() {
  $('#add').click(function(){
        $('<input type="text" name="user[]">').appendTo('#users');
    });
});

And for your current code to work use $.parseHTML:

$( document ).ready(function() {
  $('#add').click(function(){
        var newuser = $.parseHTML('Gebruiker: <input type="text" name="user[]"><br />');

        $(newuser).appendTo('#users');
    });
});

$.parseHTML is used to parse the arbitrary HTML so that it is not taken as a selector by jQuery.

Edit: adding delete feature:

$( document ).ready(function() {
    $('#add').click(function(){
        var newuser = $.parseHTML('<div><label>Gebruiker:</label> <input type="text" name="user[]"><span class="delete">Verwijderen</span></div>');
        $(newuser).appendTo('#users');
    });
    $(document).on('click','.delete', function(){ 
          $(this).parent('div').remove(); 
          console.log('reached'); 
    }); 
});

jsFiddle Example: http://jsfiddle.net/RtTpN/

Comments

0

yep, there is nothing called $.html() in jQuery. html is a method of set of nodes instead.

You can do something like:

$(document).ready(function () {
    $('#add').click(function () {
        $('#users').append('Gebruiker: <input type="text" name="user[]"><br />');
    });
});

Working Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.