Get last active element..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rytmeboksen@gmail.com

    Get last active element..

    Hi,

    I have three textareas and four buttons within a form. The buttons has
    often used text as value. When I press a button I whant the assigned
    text to be added in whatever textareas im currently in (the one with
    cursor blinking). Is this possible with Javascript?

    --
    Tommy Myrvoll

  • Ivo

    #2
    Re: Get last active element..

    <rytmeboksen@gm ail.com> wrote[color=blue]
    >
    > I have three textareas and four buttons within a form. The buttons has
    > often used text as value. When I press a button I whant the assigned
    > text to be added in whatever textareas im currently in (the one with
    > cursor blinking). Is this possible with Javascript?[/color]

    var lastarea, // global variable to remember current element
    a = document.getEle mentsByTagName( 'textarea' ), i = a.length;
    while( i-- ) {
    a[i].onfocus = setlast; // initialize
    }

    function setlast( e ) {
    e = e || window.event || {};
    var o = e.srcElement || e.target;
    lastarea = o;
    }

    Now when the button is pressed, you simply update "lastarea.value " with
    whatever the button is supposed to add.
    hth
    ivo



    Comment

    • McKirahan

      #3
      Re: Get last active element..

      <rytmeboksen@gm ail.com> wrote in message
      news:1130941314 .028571.22310@g 44g2000cwa.goog legroups.com...[color=blue]
      > Hi,
      >
      > I have three textareas and four buttons within a form. The buttons has
      > often used text as value. When I press a button I whant the assigned
      > text to be added in whatever textareas im currently in (the one with
      > cursor blinking). Is this possible with Javascript?
      >
      > --
      > Tommy Myrvoll
      >[/color]

      Will this help?

      <html>
      <head>
      <title>often.ht m</title>
      <script type="text/javascript">
      var what;
      function fokus(that) {
      what = that;
      }
      function often(that) {
      if (what == null) return;
      what.value = that.value;
      }
      </script>
      </head>
      <body>
      <form>
      <input type="reset" value="Reset">
      <br><textarea name="a1" id="a1" cols="40" rows="3"
      onfocus="fokus( this)"></textarea>
      <br><textarea name="a2" id="a2" cols="40" rows="3"
      onfocus="fokus( this)"></textarea>
      <br><textarea name="a3" id="a3" cols="40" rows="3"
      onfocus="fokus( this)"></textarea>
      <br><input type="button" value="often used text 1"
      onclick="often( this)">
      <br><input type="button" value="often used text 2"
      onclick="often( this)">
      <br><input type="button" value="often used text 3"
      onclick="often( this)">
      <br><input type="button" value="often used text 4"
      onclick="often( this)">
      </form>
      </body>
      </html>


      Comment

      • McKirahan

        #4
        Re: Get last active element..


        "McKirahan" <News@McKirahan .com> wrote in message
        news:ZKOdnRHrIe _UTfXeRVn-tA@comcast.com. ..[color=blue]
        > <rytmeboksen@gm ail.com> wrote in message
        > news:1130941314 .028571.22310@g 44g2000cwa.goog legroups.com...[color=green]
        > > Hi,
        > >
        > > I have three textareas and four buttons within a form. The buttons has
        > > often used text as value. When I press a button I whant the assigned
        > > text to be added in whatever textareas im currently in (the one with
        > > cursor blinking). Is this possible with Javascript?
        > >
        > > --
        > > Tommy Myrvoll
        > >[/color]
        >
        > Will this help?
        >
        > <html>
        > <head>
        > <title>often.ht m</title>
        > <script type="text/javascript">
        > var what;
        > function fokus(that) {
        > what = that;
        > }
        > function often(that) {
        > if (what == null) return;
        > what.value = that.value;
        > }
        > </script>
        > </head>
        > <body>
        > <form>
        > <input type="reset" value="Reset">
        > <br><textarea name="a1" id="a1" cols="40" rows="3"
        > onfocus="fokus( this)"></textarea>
        > <br><textarea name="a2" id="a2" cols="40" rows="3"
        > onfocus="fokus( this)"></textarea>
        > <br><textarea name="a3" id="a3" cols="40" rows="3"
        > onfocus="fokus( this)"></textarea>
        > <br><input type="button" value="often used text 1"
        > onclick="often( this)">
        > <br><input type="button" value="often used text 2"
        > onclick="often( this)">
        > <br><input type="button" value="often used text 3"
        > onclick="often( this)">
        > <br><input type="button" value="often used text 4"
        > onclick="often( this)">
        > </form>
        > </body>
        > </html>[/color]

        Change

        what.value = that.value;

        to

        what.value += that.value;

        to append the value to the textareas.

        Note the value is appended with adding a space
        or a line feed. Do you need either?


        Comment

        • Richard Cornford

          #5
          Re: Get last active element..

          Ivo wrote:
          <snip>[color=blue]
          > var lastarea, // global variable to remember current element
          > a = document.getEle mentsByTagName( 'textarea' ), i = a.length;
          > while( i-- ) {
          > a[i].onfocus = setlast; // initialize
          > }
          >
          > function setlast( e ) {
          > e = e || window.event || {};
          > var o = e.srcElement || e.target;
          > lastarea = o;
          > }[/color]
          <snip>

          Given that you are assigning - setlast - to the - onfocus - property of
          the textarea, so it will be called as a method of the textarea element:-

          function setlast(){
          lastarea = this;
          }

          - should be sufficient.

          Richard.


          Comment

          • rytmeboksen@gmail.com

            #6
            Re: Get last active element..


            McKirahan wrote:
            ....[color=blue][color=green]
            > > <script type="text/javascript">
            > > var what;
            > > function fokus(that) {
            > > what = that;
            > > }
            > > function often(that) {
            > > if (what == null) return;
            > > what.value = that.value;
            > > }
            > > </script>[/color][/color]
            ....

            Just what I needed. Thanks a lot

            --
            Tommy

            Comment

            Working...