A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: setFocus / getFocus

  1. #1
    Senior Member
    Join Date
    Mar 2005
    Posts
    147

    setFocus / getFocus

    Hi, I've got 6 input boxes, named input1 through input6.

    How do I get it to cycle through focus on each at the press of "tab"? Also, if possible, have the focus set on the first box at the start.

    I've done a bit of Googling, but nothing's worked.

    Any ideas?
    joechilds@imap.cc
    www.blackliquor.co.uk

  2. #2
    Senior Member chi-styler's Avatar
    Join Date
    Jul 2001
    Location
    Bristol, UK
    Posts
    1,237
    Code:
    for(var i=1; i<=6; i++) {
    	this["input" + i].tabIndex = i;
    }
    Selection.setFocus(input1);
    should do it for you. Keep in mind that the setFocus will only work if the Flash movie has focus, which is not the case when the page is first opened.

  3. #3
    Senior Member
    Join Date
    Mar 2005
    Posts
    147
    That's it, great, thanks. Can you repost that and annotate it, saying where each bit comes from and what it does, please?

    Thanks alot.
    joechilds@imap.cc
    www.blackliquor.co.uk

  4. #4
    Senior Member chi-styler's Avatar
    Join Date
    Jul 2001
    Location
    Bristol, UK
    Posts
    1,237
    To set the order of tabbing you use

    TextField.tabIndex

    A field with index of 1 will be higher up than a field with a index of 2. so in your move you essentially want to go
    Code:
    input1.tabIndex = 1;
    input2.tabIndex = 2;
    input3.tabIndex = 3;
    input4.tabIndex = 4;
    input5.tabIndex = 5;
    input6.tabIndex = 6;
    Now as your input boxes are named very similarly you can use a for loop to make this quicker and more efficient.

    for loops start a variable at a certain value and on each loop increment that variable whilst your condition is met.

    for(start variable; condition; increment) {

    you want to go from 1 to 6, so
    Code:
    for(var i=0; i<=6; i++) {
        trace("i=" + i);
    }
    makes i (your variable) start at 1(i=0), incremement by 1 on each loop (i++) and only loop whilst i is less than or equal to 6 (i<=6).

    So if you put just that code in a move and run it, you'll get in the output

    i=1
    i=2
    i=3
    i=4
    i=5
    i=6

    To assign the textfields their tabIndex you need to use this[], which is used for assigning values to dynamic objects, i.e. where part of the name is a variable (in this case it's the number on the end)

    this["input" + i]

    that'll reference each textfield each loop. then to assign it's tabIndex, it's just

    this["input" + i].tabIndex = i;

    final code:
    Code:
    for(var i=0; i<=6; i++) {   
        this["input" + i].tabIndex = i;
    }
    Hope that helps then you can annotate it yourself

    And Selection.setFocus(input1) just says focus the cursor on the TextField input1.

  5. #5
    Senior Member
    Join Date
    Mar 2005
    Posts
    147
    Yup, 5 minutes of thinking (ouch) got me there. Thanks alot.
    joechilds@imap.cc
    www.blackliquor.co.uk

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center