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];
firstname, so you can'tgetElementById.