1

I have a drop down menu of names in HTML:

<select id="names" name="nam">
<option>Generic Lastname, Firstname Middle</option>
<option>Doe Jr., Jim</option>
<option>Smith, Christopher Sam=Chris</option>
<option>Wilson, Sue Ann</option>
</select>

<input type="checkbox" id="firstname" value="" checked="checked">

I use a script to parse these names into variables split on commas and spaces. Last name, first name excluding middle names.

I'm trying to add the nicknames. Where the variable for the first name is replaced by a nickname ONLY if a nickname is present, but stays the same if the nickname variable is not present.

For example, in the above code, Christopher will be replaced by Chris, but every other option will keep their respective first names. I can't figure out how to swap the variable.

Also, do I need "=" inside every option? Or can the split function read the variable as null if no "=" is present? I'm not yet familiar with conditionals. Please be patient with me. I tried to put IFs in the code with no luck.

Here's the code:

var nam=document.forms[0].nam.value;
var half_array=nam.split(",");
var first_array=half_array[1].split(" ");
document.getElementById('firstname').value = first_array[1];
2
  • You don't appear to have an element with the id firstname, so you can't getElementById. Commented Apr 3, 2015 at 20:33
  • that element is just used elsewhere in the document to display the first name. I put first_array[1] in that spot. I'll update the original post if that's needed to be clear. My concern is about splitting the variables into arrays. Commented Apr 3, 2015 at 20:48

1 Answer 1

1

I might take a different approach to this entirely, but I'm not quite sure how you are building your data either.

The middle name, if it exists, will be the 2nd element in first_array, so you can start by checking that.

var nickname,
    nameToDisplay = first_array[1];

if (first_array[2]) {
    // split on `=` if we have a middle name
    nickname = first_array[2].split("=");
}

If the = exists, then the person's nickname will be the 1st element in nickname.

if (nickname[1]) {
    nameToDisplay = nickname[1];
}

If the = is not there, then nickname[1] will not exist and the conditional will fail.

Sign up to request clarification or add additional context in comments.

3 Comments

@user3283304 can you show me what you've tried all together? perhaps on jsfiddle.net or the like?
I can't get jsfiddle to work on my chromebook right now, but I tried your code verbatim. the function stops at the second IF statement: if (nickname[1])... is there a syntax problem there?
@user3283304 if nickname is not an array or object that will not work. You could get around this in various ways -- what makes the most sense is to put that if statement inside of the other if statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.