SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Guru coiL's Avatar
    Join Date
    Sep 2001
    Location
    QLD, Australia
    Posts
    666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy help with this script?

    Hi,

    I don't know any javascript, but I am trying to add these few lines of code to a php shoutbox that I am playing with. What I wanted to do was add clickable smilies.

    The code I've got so far:
    Code:
       <script language='javascript'>
    <!--
     function smilie(code)
     {
      opener.document.input.message.value += ' ' + code + ' ';
     }
    //-->
    </script>
    the form to post the message
    Code:
     <input type=\"text\" name=\"username\" size=\"20\" maxlength=\"25\" value=\"Name\">
    <br/>
    <input type=\"text\" name=\"message\" size=\"20\" maxlength=\"200\" value=\"Message\"> <br />
    <input type=\"submit\" name=\"tag\" value=\"  send  \"><br>
     </form>
    the code to display the smilies (that you click on)
    Code:
     			<div align=\"center\"><a href=""\"javascript:smilie(':P')\"><img src=""\"smilies/bigrazz.gif\" border=\"0\"></a></div>

    At the moment when you click on a smilie, nothing happens, but there is a error symbol in the browsers status bar.

    Could anyone help me out with this? It would be very much appreciated.
    thanks
    coiL
    "cradled in the learning curve"

  2. #2
    SitePoint Guru coiL's Avatar
    Join Date
    Sep 2001
    Location
    QLD, Australia
    Posts
    666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I discovered that I needed to name the form.
    So I did that and now it works fine.

    coiL
    "cradled in the learning curve"

  3. #3
    SitePoint Zealot
    Join Date
    May 2003
    Location
    Dover, PA
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by coiL
    The code I've got so far:
    Code:
       <script language='javascript'>
    <!--
     function smilie(code)
     {
      opener.document.input.message.value += ' ' + code + ' ';
     }
    //-->
    </script>
    FYI, there's a simpler way to do this with the DOM . . .
    Code:
     <script language="javascript" type="text/javascript"><!--
    function smilie(code) {
    document.getElementById('message').value += code;
    }
    Then, in your form give the input for message an ID.
    Code:
    <input type=\"text\" name=\"message\" size=\"20\" maxlength=\"200\" id="message" value=\"Message\"/>
    getElementById is a WONDERFUL thing.
    http://www.statgfx.com

  4. #4
    SitePoint Addict
    Join Date
    May 2003
    Location
    The Netherlands
    Posts
    391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You do not need any document.getElementById to refer to a form element. Actually, it is even wrong since you are limiting the support for your own script.

    The form collection is an array you can access through document.formname in all JavaScript versions. Why would you not let old vesions run the script?

  5. #5
    SitePoint Zealot
    Join Date
    May 2003
    Location
    Dover, PA
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nacho
    You do not need any document.getElementById to refer to a form element. Actually, it is even wrong since you are limiting the support for your own script.

    The form collection is an array you can access through document.formname in all JavaScript versions. Why would you not let old vesions run the script?
    Well, it is an alternative way of doing things. No, you don't need to use it, just like you don't need to use CSS or any other thing. It's very convenient though and is supported by all major recent browsers; so I don't think it's greatly limiting the support. If you're coding for NS 4.x support, then don't use it. I just find it extremely convenient in many situations.
    http://www.statgfx.com

  6. #6
    SitePoint Addict
    Join Date
    May 2003
    Location
    The Netherlands
    Posts
    391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why would you change a winning team?

  7. #7
    SitePoint Zealot
    Join Date
    May 2003
    Location
    Dover, PA
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nacho
    Why would you change a winning team?
    Well, they're both winners in my opinion. They both work.

    On a side note, I found this related thread on the topic.
    http://www.statgfx.com

  8. #8
    SitePoint Addict
    Join Date
    May 2003
    Location
    The Netherlands
    Posts
    391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    They are both winners to you because you only look to new browser versions.

    As a good developer however you should keep using the collection document.forms whenever possible as it is a recommendation of W3C.

    If you are just a commercial developer and believe programming is a trend then use whatever pleases you. You should know though that you will never make it to the top.

  9. #9
    SitePoint Zealot
    Join Date
    May 2003
    Location
    Dover, PA
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nacho
    They are both winners to you because you only look to new browser versions.

    As a good developer however you should keep using the collection document.forms whenever possible as it is a recommendation of W3C.

    If you are just a commercial developer and believe programming is a trend then use whatever pleases you. You should know though that you will never make it to the top.
    True, I don't develop for NS 4.x or IE 4. On the other hand, I wouldn't use certain things if I knew they would cause problems for a large part of my target audience.

    I'm not saying you should forget it; I was just saying it's an alternative. Both are part of W3C recommendations.

    I didn't post the alternative and wouldn't use it just to 'please myself.' Sure, it is easy to use; that doesn't mean I wouldn't use the olther method. Either way, I think it was helpful to offer an alternative, if someone is unaware of getElementById. Even if this is not the best example of using getElementById, it has a lot of use in other situations.
    http://www.statgfx.com


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
  •