SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Jul 2007
    Location
    San Jose, California
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Not sure this is best way

    I have fields of the form:
    field_name_tag_1
    field_name_tag_2
    and so on.

    I want to be able to get the next field so if you click on field_name_tag_3 you should then goto field_name_tag_4.

    currently, i'm doing this like this:
    temp = field.id.split("_");
    temp[3] = 1*temp[3] + 1;
    temp = temp.join('_');

    Where field.id is field_name_tag_x.

    I feel that a regular expression would be better but i'm not sure how to write one.

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by baileylo View Post
    I want to be able to get the next field so if you click on field_name_tag_3 you should then goto field_name_tag_4.
    Could you clarify the meaning of this, since to me it's unclear. Do you mean if field_name_tag_3 is focused, the focus is set to field_name_tag_4? If so, under what circumstances?
    Tab-indentation is a crime against humanity.

  3. #3
    SitePoint Addict
    Join Date
    Jul 2007
    Location
    San Jose, California
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes. What happens is you fill out field x and a function handles the data and then sets the focus to field x+1.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Here's a one-liner for incrementing the last digits of a string.

    Code javascript:
    fieldid = fieldid.replace(/\d+$/, parseInt(/\d+$/.exec(fieldid))+1);

    but it really should be expanded to something more understandable, such as:

    Code javascript:
    var fieldid = 'field_name_tag_1';
    var lastDigits = /\d+$/;
    var nextDigits = parseInt(lastDigits.exec(fieldid)) + 1;
    fieldid = fieldid.replace(lastDigits, nextDigits);
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by baileylo View Post
    yes. What happens is you fill out field x and a function handles the data and then sets the focus to field x+1.
    If you pass this function a reference to the currently focused field, it will set the focus to a field having the same name prefix and a consecutive numeric suffix:
    Code:
    <script type='text/javascript'>
    
    function jumpToNextField(field)
    {
     var m, next;
       
     if( (m=field.name.match(/(.*?)(\d+)$/) ) )
      if( (next = field.form[m[1] + (Number(m[2])+1)] ) )
       next.focus();
    }
    
    </script>
    You don't say how your data handling code is triggered, but using the onblur event with this functionality could confuse the user.
    Tab-indentation is a crime against humanity.


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
  •