SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    lisboa
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    function wont work

    i've this code, but the output is:
    hmm (in lowercase and plain black...)
    HTML Code:
     <script type = "text/javascript">
      var astring = "";
      function teste(astring) {
       astring.toUpperCase();
       astring.fontcolor('red');
       return astring;
      }
     </script>
     
     </head>
     <body>
     
     <script language = "JavaScript">
      document.writeln(teste("hmm"));
     </script>
    why this does not work?

    TiA

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    toUpperCase() and other String methods do not modify the string they're applied to, but return the modified copy of it. Therefore you need to save the result in a variable:

    Code:
    var newString = astring.toUpperCase();
    return newString;

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    lisboa
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog View Post
    toUpperCase() and other String methods do not modify the string they're applied to, but return the modified copy of it. Therefore you need to save the result in a variable:

    Code:
    var newString = astring.toUpperCase();
    return newString;
    i'm sorry if i look dumb, but i dont understand it; if i make:
    HTML Code:
    return astring.toUpperCase().fontcolor('red');
    i get it right
    why then?

    TiA

  4. #4
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    lisboa
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ty!


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
  •