How to format data selected from a ListBox


When I use this Javascript function

  var ddl = document.getElementById("ddlName");
  var index = ddl.selectedIndex;

  document.getElementById("iframe2").contentDocument.getElementById("txtName").value = ddl.options[index].text;

to post ListBox Data to my iframe, it gives me the full value shown in the listbox which is something formatted like:

001-010008 - Bill French

In another field, how can I use the Javascript split function to only show me everything after the ID number, 001-010008 OR

Is there a better way to do this?
I still want to keep a field with the old formatted data.

Sample code would be very helpful.


Thanks for the explaination, and it worked as intended.


ddl.options[index].text.match(/ - (\\D+)$/);
document.getElementById("iframe2").contentDocument.getElementById("txtName").value = RegExp.$1;

It returns array that consists of the matched text from the regex, and then the grouped matches that are within the parenthesis.

You’re wanting only the grouped match, so you’ll want the [1] index from the result.

Also, instead of turning it in to a one-liner, it can be easier to understand what’s going on by working out the name first, then assigning that name to the appropriate field.

var name = ddl.options[index].text.match(/ - (\\D+)$/)[1];
document.getElementById("txtName2").value = name;


I’m sure that I’m missing something here.
Was I supposed to make a var out of this like

var RegExp =ddl.options[index].text.match(/ - (\D+)$/);
and then call it like this?
document.getElementById(“iframe2”).contentDocument.getElementById(“txtName”).value = RegExp.$1;

That produces undefined

If I use it like this
document.getElementById(“txtName2”).value = ddl.options[index].text.match(/ - (\D+)$/);

Then I get a result like - Match NM,Match NM

It repeats the name.
How can this be corrected?