www.webdeveloper.com
Results 1 to 6 of 6

Thread: can i use the function, and condition?

  1. #1
    Join Date
    May 2013
    Posts
    4

    can i use the function, and condition?

    excuse me.. I am a newbie in javascript, Can you help me? I want to make a link with <a href="blabla" onclick="myfunction()">HERE</a> and the HERE must be changed three times, HERE (onclick being NEXT), NEXT (oclick being NOW), and NOW (onclick being STARTS). Please tell me the code, thanks..

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,351
    Please tell us the purpose ...

  3. #3
    Join Date
    May 2013
    Posts
    4
    sorry,i mean this TEXT link MUST CHANGES when i click on this link. In html:<a id="changes of the text" href="#" ONCLICK="myFunction()">FIRST word</a>
    and when i click on those link, the text must change being SECOND word and so on until five times. Some hours ago i have learn with my self and i found thd answer with the CONDITIONAL.
    here my code in javascript:
    <script type="text/javascript">
    function myFunction()
    {
    var a='first', b='second', c='third', d='fourth', e='fifth;
    f=document.getElementById('changes of the text');
    if (f.innerHTML.match(a))
    {
    f.innerHTML=b;
    }
    else if (f.innerHTML.match(b))
    {
    f.innerHTML=c;
    }
    else
    {
    f.innerHTML=d;
    }
    {
    and what the code to change var d being var e?
    {
    and what the code to change var e being var a?
    }
    }
    </script>
    i have try to put the if and the else if again, but it doesn't work. Please tell me.. Thanks..

  4. #4
    Join Date
    May 2013
    Posts
    4
    thanks for the responce

  5. #5
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,351
    Not much of your syntax is correct so I just re-wrote it to work.
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> Untitled </title>
    
    </head>
    <body>
    <button onclick="myFunction()">Change of Text</button>
    <br> <div id="changes_of_the_text">first</div>
    
    <script type="text/javascript">
    function myFunction() {
      var changeContents = ['first', 'second', 'third', 'fourth', 'fifth'];
      f=document.getElementById('changes_of_the_text');
      var count = 0;
      while (count < changeContents.length-1) {
        if (f.innerHTML.match(changeContents[count])) {
          count++;
          f.innerHTML=changeContents[count];
          count = changeContents.length;
        } else {count++; }
      }
    }
    </script>
    </body>
    </html>
    BTW: You should enclose your script between [ code] and [ /code] tags (without the spaces)
    to make it easier for forum members to read, copy test, etc.

  6. #6
    Join Date
    May 2013
    Posts
    4
    thank you JMRKER, its really working on my browser

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles