I have this function here:
function nameSplit() {
var option_result = document.getElementById("fname").value;
var option_array=option_result.split(" ");
document.getElementById('first_name').value = option_array[0];
document.getElementById('last_name').value = option_array[1];
}
function myFunction() {
var option_result = document.getElementById("fname").value;
document.getElementById("demo").innerHTML = option_result.length;
document.getElementById("demo2").innerHTML = option_array.prototype.length;
}
<form>
<label>Name:</label>
<input type="text" id="fname">
<input type='button' onclick='nameSplit()' value='Change Text'/>
</form>
<br>
<input id="first_name" name="fid" value="" />
<input id="last_name" name="sid" value="" />
<br>
option_result: <p id="demo"></p>
option_array: <p id="demo2"></p>
<button onclick="myFunction()">Try it</button>
As you can see my option_array.prototype.length;
is not returning anything. So I don't know the best way the explain this but in short I want to determine how many arrays are within the function and have that as the number that is being pulled at
document.getElementById('last_name').value = option_array[1];
I want it to work like this:
document.getElementById('last_name').value = option_array[(Number of total arrays)];
If you run the original function and type "John Doe" it will split it into "John" and "Doe", but if you change the input to "John Doe Jr." it will still return with "John" and "Doe". I want it to return "John" and "Doe Jr."
I have tried to create a script that runs option_array[1] + " " + option_array[2];
but if a person only uses two parts of the array, the third comes back undefined.
Any ideas?