can you set focus to a custom custom element?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phl

    #1

    can you set focus to a custom custom element?

    hi,

    I can use the following code to set focus to standard html elements
    but if I try to do the same for a custom element, it seems to complain
    that it does not support a focus method. Does anyone know what is the
    problem, if there is a work around?

    thank you


    <SCRIPT language='javas cript'>
    document.getEle mentById('" + ctrl.ID + "').focus()
    </SCRIPT>
  • Steve Pugh

    #2
    Re: can you set focus to a custom custom element?

    killkennyhouse@ hotmail.com (Phl) wrote:
    [color=blue]
    >I can use the following code to set focus to standard html elements
    >but if I try to do the same for a custom element, it seems to complain[/color]

    What is 'it'? A browser? If so, which one?
    [color=blue]
    >that it does not support a focus method. Does anyone know what is the
    >problem, if there is a work around?[/color]

    A quick test shows that IE6 responds in exactly the same way to
    document.getEle mentsByTagName( 'foo')[0].focus()
    as it does to
    document.getEle mentsByTagName( 'div')[0].focus()

    So there seems to be no problem with applying the focus() method to
    custom elements. Whether there's any effect from the user's point of
    view is another matter.
    [color=blue]
    ><SCRIPT language='javas cript'>
    >document.getEl ementById('" + ctrl.ID + "').focus()
    ></SCRIPT>[/color]

    Can you post a URL? Without seeing the rest of your code it's hard to
    tell what the problem is. The code you have there looks odd.
    There's a good chance that your problem is JavaScript rather than HTML
    related, in which case comp.lang.javas cript would offer the best
    chance for helpful advice.

    Steve

    --
    "My theories appal you, my heresies outrage you,
    I never answer letters and you don't like my tie." - The Doctor

    Steve Pugh <steve@pugh.net > <http://steve.pugh.net/>

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: can you set focus to a custom custom element?

      Phl schrieb:[color=blue]
      > I can use the following code to set focus to standard html elements[/color]

      Not for all elements in all UAs as both their properties and DOMs differ.
      [color=blue]
      > but if I try to do the same for a custom element, it seems to complain
      > that it does not support a focus method. Does anyone know what is the
      > problem, if there is a work around?
      > [...]
      > <SCRIPT language='javas cript'>
      > document.getEle mentById('" + ctrl.ID + "').focus()
      > </SCRIPT>[/color]

      The proper syntax would be

      <script type="text/javascript">
      document.getEle mentById(ctrl.I D).focus()
      </script>

      This works provided that there is an element with an ID equal to the
      value of ctrl.ID and document.getEle mentById() returns a reference to
      an object that has a focus() method. So this is error-prone and one
      should at least write

      <script type="text/javascript">
      if (typeof document != "undefined"
      && document.getEle mentById)
      {
      var o = document.getEle mentById(ctrl.I D);
      if (o && o.focus)
      {
      o.focus();
      }
      }
      </script>

      (whatever ctrl.ID may be and where it may be defined).


      F'up2 comp.lang.javas cript (DO NOT crosspost without Followup-To!)

      PointedEars

      Comment

      • Phl

        #4
        Re: can you set focus to a custom custom element?

        here is the custom element I am trying to access:

        <prog:ComboBo x id="insert_prjc ode" name="insert_pr jcode"
        ParentFormID="F orm1"
        ResourcesDirect ory="/webctrl_client/progstudios/1_2/" value="1"
        size="5" onchange="__doP ostBack('insert _prjcode','')">


        here is the javascript which i used to access but fails , when i try
        and use the onfocus method:

        <SCRIPT language='javas cript'>var cobj =
        document.getEle mentById('inser t_prjcode');
        if(!cobj){alert ('failed');}els e{alert('passed ');cobj.focus() ;}

        Does this make the problem clearer?

        thx

        Comment

        • Phl

          #5
          Re: can you set focus to a custom custom element?

          here is the custom element I am trying to access:

          <prog:ComboBo x id="insert_prjc ode" name="insert_pr jcode"
          ParentFormID="F orm1"
          ResourcesDirect ory="/webctrl_client/progstudios/1_2/" value="1"
          size="5" onchange="__doP ostBack('insert _prjcode','')">


          here is the javascript which i used to access but fails , when i try
          and use the onfocus method:

          <SCRIPT language='javas cript'>var cobj =
          document.getEle mentById('inser t_prjcode');
          if(!cobj){alert ('failed');}els e{alert('passed ');cobj.focus() ;}

          Does this make the problem clearer?

          thx

          Comment

          • Harlan Messinger

            #6
            Re: can you set focus to a custom custom element?


            "Phl" <killkennyhouse @hotmail.com> wrote in message
            news:55863e96.0 406160751.48610 83b@posting.goo gle.com...[color=blue]
            > here is the custom element I am trying to access:
            >
            > <prog:ComboBo x id="insert_prjc ode" name="insert_pr jcode"
            > ParentFormID="F orm1"
            > ResourcesDirect ory="/webctrl_client/progstudios/1_2/" value="1"
            > size="5" onchange="__doP ostBack('insert _prjcode','')">
            >
            >
            > here is the javascript which i used to access but fails , when i try
            > and use the onfocus method:
            >
            > <SCRIPT language='javas cript'>var cobj =
            > document.getEle mentById('inser t_prjcode');
            > if(!cobj){alert ('failed');}els e{alert('passed ');cobj.focus() ;}
            >
            > Does this make the problem clearer?[/color]

            What is a custom element for, and how does a client even know how to render
            it, let alone indicate that it has the focus?

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: can you set focus to a custom custom element?

              Phl wrote:[color=blue]
              > here is the custom element I am trying to access:
              >
              > <prog:ComboBo x id="insert_prjc ode" name="insert_pr jcode"
              > ParentFormID="F orm1"
              > ResourcesDirect ory="/webctrl_client/progstudios/1_2/" value="1"
              > size="5" onchange="__doP ostBack('insert _prjcode','')">[/color]

              This is not HTML, so what markup language is this?
              [color=blue]
              > here is the javascript which i used to access but fails , when i try
              > and use the onfocus method:[/color]

              You mean the _focus_ method.
              [color=blue]
              > <SCRIPT language='javas cript'>var cobj =
              > document.getEle mentById('inser t_prjcode');
              > if(!cobj){alert ('failed');}els e{alert('passed ');cobj.focus() ;}[/color]

              I have already posted the correct syntax and less
              error-prone solution. Why don't you try that first?
              [color=blue]
              > Does this make the problem clearer?[/color]

              Alas not. Besides, if the object has no focus() method, it cannot work.


              PointedEars, F'up2 cljs

              P.S.:
              Please stop crossposting (without followup-To). This has nothing to do
              with HTML and thus it is off-topic in ciwa.html.

              Comment

              • Grant Wagner

                #8
                Re: can you set focus to a custom custom element?

                Phl wrote:
                [color=blue]
                > here is the custom element I am trying to access:
                >
                > <prog:ComboBo x id="insert_prjc ode" name="insert_pr jcode"
                > ParentFormID="F orm1"
                > ResourcesDirect ory="/webctrl_client/progstudios/1_2/" value="1"
                > size="5" onchange="__doP ostBack('insert _prjcode','')">
                >
                > here is the javascript which i used to access but fails , when i try
                > and use the onfocus method:
                >
                > <SCRIPT language='javas cript'>var cobj =
                > document.getEle mentById('inser t_prjcode');
                > if(!cobj){alert ('failed');}els e{alert('passed ');cobj.focus() ;}
                >
                > Does this make the problem clearer?
                >
                > thx[/color]

                You aren't using the "onfocus" method, you're using the "focus" method.
                However, you aren't testing to see whether the object supports the
                focus() method before you attempt to use it.

                Start with this:

                <script type="text/javascript">
                var cobj = document.getEle mentById('inser t_prjcode');
                if (cobj) {
                alert(cobj.focu s);
                }
                </script>

                If you get no alert, the object isn't being retrieved correctly.

                If you get the alert(), but see anything other then:

                function focus() {
                [native code]
                }

                then the focus method isn't supported by your object. Even if you get
                the text described above, the "native code" may not actually set the
                focus, or it may attempt to set the focus, but other issues within the
                user agent prevent that from happening. Once you have determined that
                the object supports the focus() method, then the proper test for usage
                would be:

                if (cobj && cobj.focus) {
                cobj.focus();
                }

                --
                | Grant Wagner <gwagner@agrico reunited.com>

                * Client-side Javascript and Netscape 4 DOM Reference available at:
                *


                * Internet Explorer DOM Reference available at:
                *
                http://msdn.microsoft.com/workshop/a...ence_entry.asp

                * Netscape 6/7 DOM Reference available at:
                * http://www.mozilla.org/docs/dom/domref/
                * Tips for upgrading JavaScript for Netscape 7 / Mozilla
                * http://www.mozilla.org/docs/web-deve...upgrade_2.html


                Comment

                Working...