0

Learning to code and having a problem creating arrays from jQuery objects. I would like to give the user the option of adding as many "Favorite Books" as they want to their profile.

The UI is written in jQuery,

<script language="javascript">
    function one()
    {
        var i = 1;
        my.innerHTML = my.innerHTML +"<br><input type='text' name='title'+i[] >"
        var n = 1;
        [
        div.innerHTML = div.innerHTML +"<br><input type='text' name='author'+n[] >"
        ]
    }
</script>

I have tried:

var obj = $('input');
var arr = $.makeArray(obj);

I was hoping it was actually that easy but the output is:

<div id="div">
<br>
<input type="text" +n[]="" name="author">
<br>
<input type="text" +n[]="" name="author">
<br>
<input type="text" +n[]="" name="author">
</div>

I have tried option number two which I found here yet gave me the same output:

var author = new Array();

//get all the authors
$('.auth input').each(function (i)
{
    var author= $(this).val();

    if(author!= '')
    {
        author[author] = author;
        alert(author.length);
    }
});

and same results. I was hoping for results like this:

<div id="div">
<br>
<input type="text" name="author[0]">
<br>
<input type="text" name="author[1]">
<br>
<input type="text" name="author[2]">
</div>

so I can parse out to a PHP array.

Travis
  • 12,001
  • 8
  • 39
  • 52
n1c0
  • 89
  • 1
  • 2

1 Answers1

0

Maybe:

<script language="javascript">
    var i = 0;
    function one(){
       $('#my').append("<br><input type='text' name='title["+i+"]'>");
       $('#div').append("<br><input type='text' name='author["+i+"]'>");
       i++;
    }
</script>
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107