-1

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?

Jongware
  • 22,200
  • 8
  • 54
  • 100
Rick Sibley
  • 605
  • 7
  • 18
  • Your innerhtml lines are a fair attempt of debugging, but they don't seem to do anything useful. (I'm not sure that `prototype.length` line does what you think it should.) What if you add a straightforward test for `option_array.length` - the usual way of checking an array's length? – Jongware Feb 02 '16 at 18:20
  • Ive tried option_array.length and I get a blank result as well. – Rick Sibley Feb 02 '16 at 18:35
  • Something iffy is going on then. Best check if (1) your `option_array` makes it into this function, and (2) *anything* else *does* appear in that position. See, by the way [Mozilla's documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length) for examples of checking an arrays' length. – Jongware Feb 02 '16 at 18:39
  • Wow. I forgot to add `var option_array=option_result.split(" ");` into function `myFunction()`. After i did that and then ran `document.getElementById("demo2").innerHTML = option_array.length;` it came back with the number of arrays. – Rick Sibley Feb 02 '16 at 19:18
  • I went on to write the _if else_ statement based on the new information I gained. Im having another issue now, do you have any ideas @Jongware [JSfiddle Link](https://jsfiddle.net/s5yhhyun/2/) – Rick Sibley Feb 02 '16 at 19:48

1 Answers1

1

Well I thought this function would be a great way to retrieve the information I needed but my boss stop by my desk and told me there was an easier way, and gave me some information to do research on, and so I went on to build a new one..

Here is the code for anyone interested:

function nameSplit() {
        var first_name = document.getElementById("first_name");
        var last_name = document.getElementById("last_name");
        var fname = document.getElementById("fname");
        var myfname = fname.value.trim();
    if (myfname.indexOf(" ") < 0)
    {
        first_name.value = fname.value.trim();
        last_name.value = "Undefined";
    } else {
        first_name.value = fname.value.substring(0, fname.value.indexOf(" "));
        last_name.value = fname.value.substring((fname.value.indexOf(" ") + 1), fname.value.length);
    }
}
<form>
    <label>Name:</label> 
    <input type="text" id="fname">
    <input type='button' onclick='nameSplit()' value='Submit'/>   
</form>
<br>
    <input id="first_name" value="" />
    <input id="last_name" value="" />

Thanks for your help @jongware

Rick Sibley
  • 605
  • 7
  • 18