最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Single array[0] item is returning undefined - Stack Overflow

programmeradmin4浏览0评论

all devs smarter than me,

So basically I've got a name(first) input field, I want to let the user enter their first name and last name into that input. After that, I want to take the 2nd value(last name) and push the value into another input which is hidden(display:none). So the code below, in theory, should do it

var array = [];

var fullname = $("#firstName").value.split(" ");

array.push(fullname);

$("#lastName").value = array[1];

alert(array[1])

For example iI i put in "First Last" as the name values in the firstName input and split and push to array, if I alert it alert(array) or $("#lastName").value = array, it does push the correct values, however when I try to alert or set only the 2nd part of the array[1] it keeps ing back as undefined!? Please enlighten me my friends.

all devs smarter than me,

So basically I've got a name(first) input field, I want to let the user enter their first name and last name into that input. After that, I want to take the 2nd value(last name) and push the value into another input which is hidden(display:none). So the code below, in theory, should do it

var array = [];

var fullname = $("#firstName").value.split(" ");

array.push(fullname);

$("#lastName").value = array[1];

alert(array[1])

For example iI i put in "First Last" as the name values in the firstName input and split and push to array, if I alert it alert(array) or $("#lastName").value = array, it does push the correct values, however when I try to alert or set only the 2nd part of the array[1] it keeps ing back as undefined!? Please enlighten me my friends.

Share Improve this question edited Apr 26, 2018 at 10:11 Gufran Hasan 9,4117 gold badges43 silver badges55 bronze badges asked Apr 26, 2018 at 9:34 Jim41MavsJim41Mavs 5721 gold badge5 silver badges25 bronze badges 7
  • 1 use .val() instead of .value? – Eddie Commented Apr 26, 2018 at 9:35
  • 1 Your array will be like [["the", "splitted", "value"]]. You need to use array = fullname instead of push() – Weedoze Commented Apr 26, 2018 at 9:35
  • 2 There's no need for the array variable. fullname is the array you want, and fullname[1] is their last name. – Barmar Commented Apr 26, 2018 at 9:37
  • ...and also val(fullname[1]) rather than value = fullname[1]. – T.J. Crowder Commented Apr 26, 2018 at 9:37
  • 1 This script shouldn't work at all... .value will be undefined hence .value.split(" ") will throw a type error... – Andreas Commented Apr 26, 2018 at 9:39
 |  Show 2 more ments

4 Answers 4

Reset to default 6

Firstly, jQuery uses the val() method to retrieve the value of a form element. Attempting to access the value property will return nothing.

That aside, your issue is because you're creating a nested array by pushing the result of split() (ie. an array) in to array:

var fullname = $("#firstName").val().split(" ");

var array = [];
array.push(fullname);
console.log(array)
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="firstName" value="Foo Bar" />

Instead, just work with the result of split() directly, without calling push():

var fullname = $("#firstName").val();

var array = fullname.split(' ');
console.log(array[1]);
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="firstName" value="Foo Bar" />

This is obviously a basic example. You would also need to ensure that the resulting array has at least 2 elements in it.

you have some syntax issues as:

var array = [];

var fullname = $("#firstName").val();
array=fullname.split(" ");
$("#lastName").val(array[1]);
alert(array[1]);

You want to push an array values to an array, instead of array.push(fullname);, use

array.push.apply( array , fullname);

or

array = array.concat(fullName);

In your code, use $("#lastName").val() instead of $("#lastName").value. You are using array[] instead of array constructor(). E.g: var a = array[1] returns 1 which means value of a = 1.

Kindly check the link: Difference between new Array() and [ ]

发布评论

评论列表(0)

  1. 暂无评论