SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to format data selected from a ListBox

    Hi:

    When I use this Javascript function

    Code:
      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

  2. #2
    SitePoint Zealot
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    ddl.options[index].text.match(/ - (\D+)$/);
    document.getElementById("iframe2").contentDocument.getElementById("txtName").value = RegExp.$1;

  3. #3
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    dogFang:

    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?

    Thanks

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    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.

    Code javascript:
    var name = ddl.options[index].text.match(/ - (\D+)$/)[1];
    document.getElementById("txtName2").value = name;
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the explaination, and it worked as intended.

    Great!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •