See https://api.jquery.com/toArray/

$( "li" ).toArray()

This works great in cases like

$( "li" ).toArray().each( function(li) {
  console.log('do something with this list item', li);
})

or if you really want

$( "li" ).toArray()[0]
Answer from Paul Schwarz on Stack Overflow
🌐
jQuery
api.jquery.com › toArray
.toArray() | jQuery API Documentation
.toArray() returns all of the elements in the jQuery set: All of the matched DOM nodes are returned by this call, contained in a standard array:
🌐
jQuery
api.jquery.com › get
.get() | jQuery API Documentation
With an index specified, .get( index ) retrieves a single element: Since the index is zero-based, the first list item is returned: ... Each jQuery object also masquerades as an array, so we can use the array dereferencing operator to get at the list item instead:
🌐
jQuery
api.jquery.com › map
.map() | jQuery API Documentation
As the return value is a jQuery object, which contains an array, it's very common to call .get() on the result to work with a basic array.
Find elsewhere
Top answer
1 of 3
8

Your selector:

$("input[id^="author"]")

is incorrect, because you're trying to use double quotes inside double quotes. One of the pairs needs to be single quotes.

If you were using a JS debugger this should be immediately visible.

FWIW, this is a cleaner way of retrieving the required array:

var authors = $('input[id^="author"]').map(function() {
  return this.value;
}).get();
2 of 3
3

Your "html array" is not an array at all, even assuming there are a number of other similar inputs (though when the form is submitted your chosen server-side language likely lets you access the values of inputs with the same name as an array). Anyway...

The main problem is that your JS has a syntax error:

$("input[id^="author"]")

If you want to include double-quotes inside a double-quoted string literal you have to do escape them:

$("input[id^=\"author\"]")

Or quote the string with singles:

$('input[id^="author"]')

It may be more efficient to avoid the attribute starts-with selector with the id attribute and select by the name attribute, noting that for jQuery the square brackets in the atttribute name need to be escaped with backslashes, and for JS string literals backslashes also need to be escaped:

$('input[name="authors\\[\\]"]')

Also to get the value of an input with jQuery use .val() rather than .text(), but it is more efficient to just use this.value:

var authors = [];
$('input[id^="author"]').each(function(){ authors.push(this.value); }); 

I'd also recommend declaring arrays with = [] rather than = new Array().

🌐
EDUCBA
educba.com › home › software development › software development tutorials › jquery tutorial › jquery array
jQuery Array | A Complete Guide to the jQuery Array
April 19, 2023 - For this we will be using document object to fetch the list of details from HTML DOM using either id or class selector or using method getElementsByTagName and mentioning the tag which is defined in HTML.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
GeeksforGeeks
geeksforgeeks.org › jquery › how-to-use-array-with-jquery
How to use array with jQuery ? - GeeksforGeeks
December 20, 2023 - ... jQuery provides a generic .each() method to iterate over elements of arrays, as well as properties of objects. In the case of an array, the callback is passed an array index and a corresponding array value each time.
🌐
SitePoint
sitepoint.com › blog › javascript › jquery array search simple example
jQuery Array Search Simple Example — SitePoint
February 29, 2024 - Quick example on how you can use jQuery’s $.map() function to grab a value from an array of JS objects.
🌐
W3Schools
w3schools.com › jquery › misc_toarray.asp
jQuery Misc toArray() Method
The toArray() method returns the elements matched by the jQuery selector as an array.
🌐
jQuery
api.jquery.com › jQuery.inArray
jQuery.inArray() | jQuery API Documentation
Description: Search for a specified value within an array and return its index (or -1 if not found).