0

Hello Stack Overflow i hope you are well today,

I have this code:

  $('.add_child').click(function(){
    var attrName = $(this).attr('name');
    var count = $(this).attr('count');
    $(this).attr('count', (parseInt(count)+1))
    var input = $('<input />');
    input.attr('type','text')
    input.attr('name',attrName+"["+count+"]" ) 
    $('.children_form').append('<li>'+[input]+'</li>');
})

i am trying to get my out to display as:

<li><input /></li>

But it doesn't seem to work properly i am getting this code in my HTML

<li>[object Object]</li>

I believe this line is giving me the issue:

$('.children_form').append('<li>'+[input]+'</li>');
2
  • What do you get if you remove the square brackes ([ and ]) from around the input in that final line? Commented Mar 16, 2011 at 17:25
  • @David Thomas the same as before <li>[object object]</li> Commented Mar 16, 2011 at 17:28

2 Answers 2

2

With [input] you are creating an array. But '<li>'+input+'</li>' won't work either, because input is an object, not HTML.

You'd have to do like so:

$('.children_form').append($('<li />').append(input));

count is not a valid HTML attribute. You should use .data() instead. You can set the initial value with an attribute in HTML5 data attributes format: data-count="0".

Sign up to request clarification or add additional context in comments.

3 Comments

Winner, Winner, Chicken Dinner !
@Xavier: I like that movie :)
was a great film, well Kevin was awsome in the film :D
0
$('.add_child').click(function(){
    var attrName = $(this).attr('name'),
        count = $(this).attr('count');

    $(this).attr('count', (parseInt(count)+1));

    var input = $('<input />', {
        type: 'text',
        name: attrName + '[' + count + ']',
    });
    $('.children_form').append($('<li>').html(input));
});

1 Comment

@Felix, no sir. It accepts jQuery Objects too. You can try it by yourself.

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.