SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2007
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cursor at the begining of textarea on tab

    <Script Language=JavaScript>

    function setSelRange(inputEl, selStart, selEnd) {
    if (inputEl.setSelectionRange) {
    inputEl.focus();
    inputEl.setSelectionRange(selStart, selEnd);
    } else if (inputEl.createTextRange) {
    var range = inputEl.createTextRange();
    range.collapse(true);
    range.moveEnd('character', selEnd);
    range.moveStart('character', selStart);
    range.select();
    }
    }
    </script>

    The above script sets the cursor position in the textarea at the begining of text. The textarea is already filled with text and I need to edit it, so I can start editing from the top. The scripts work fine.

    <textarea rows=15 cols=100 name='POST' onfocus='setSelRange(document.multiupdate.POST, 0, 0)' />

    The problem is that I have multiple textareas with names like POST[$i] in the same form. Multiple textarea being submitted to PHP script as an array. So now when I call the function in different textboxes, it doesn't work.

    <form name=multiupdate method="POST">

    <textarea rows=15 cols=100 name='POST[0]' onfocus='setSelRange(document.multiupdate.POST[0], 0, 0)' />

    <textarea rows=15 cols=100 name='POST[1]' onfocus='setSelRange(document.multiupdate.POST[1], 0, 0)' />

    <textarea rows=15 cols=100 name='POST[2]' onfocus='setSelRange(document.multiupdate.POST[2], 0, 0)' />

    How to overcome these textarea names with square brackets [] in it?

    I am a complete newbie when it comes to javascript and have taken this function from another site.

    Can someone please help?

  2. #2
    SitePoint Enthusiast TriLLi's Avatar
    Join Date
    Feb 2010
    Location
    BiH, Bugojno
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi,

    create id attribute for textarea,
    for example one, two, three.
    Name is used only when you submit your data, and only fields which contains name will be submited to server. So you can use id instead, they must be different.

    But also you can do it like this:

    Example:
    Code HTML4Strict:
    <form name=multiupdate method="POST">
     
    <textarea rows=15 cols=100 name='POST[0]' onfocus='setSelRange(this, 0, 0)' />
     
    <textarea rows=15 cols=100 name='POST[1]' onfocus='setSelRange(this, 0, 0)' />
     
    <textarea rows=15 cols=100 name='POST[2]' onfocus='setSelRange(this, 0, 0)' />
    </form>
    http://www.wiseblog.info
    Programmers don't die, they just GO SUB without return.

  3. #3
    om nom nom nom Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,266
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    That's an option, but mixing markup and scripting behaviour is usually undesirable. And the form needs a block child.


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
  •