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.
-
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 usearray = fullname
instead ofpush()
– Weedoze Commented Apr 26, 2018 at 9:35 -
2
There's no need for the
array
variable.fullname
is the array you want, andfullname[1]
is their last name. – Barmar Commented Apr 26, 2018 at 9:37 -
...and also
val(fullname[1])
rather thanvalue = fullname[1]
. – T.J. Crowder Commented Apr 26, 2018 at 9:37 -
1
This script shouldn't work at all...
.value
will beundefined
hence.value.split(" ")
will throw a type error... – Andreas Commented Apr 26, 2018 at 9:39
4 Answers
Reset to default 6Firstly, 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 [ ]