www.webdeveloper.com
Results 1 to 8 of 8

Thread: for loop not working

  1. #1
    Join Date
    May 2013
    Location
    India
    Posts
    4

    Question for loop not working

    Dear all,

    I want to highlight first 10 nofollow links on my webpage and I am using the below javascript code

    <script type="text/javascript">
    function highlight()
    {
    var attr = document.getElementsByTagName("a")[0];
    if (attr.getAttributeNode("rel").value=="nofollow")
    {
    attr.style.color = "#00a3cc";
    }
    }

    function loop()
    {
    for (var i=0; i<=10; i++)
    {
    highlight();
    }
    }
    </script>

    It just work fine for the first nofollow link, rest remains unchanged, it means the loop is not working.
    What needs to be modified? I'm calling loop() function on body load.

    please help

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by csshacks View Post
    It just work fine for the first nofollow link, rest remains unchanged, it means the loop is not working.
    No it does not mean that.

    It means the function is doing the same thing each time you call it. Instruct it to do something different each time.
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  3. #3
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,636
    Code:
    function loop(){
    var aa = document.getElementsByTagName("a"),counter=0;
    for(var i=0; i<aa.length; i++){
    if(aa[i].rel && aa[i].rel=="nofollow"){if(counter<11){aa[i].style.color="#00a3cc";counter++;}}
    else{continue;}
    } 
    }
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  4. #4
    Join Date
    May 2013
    Location
    India
    Posts
    4

    Thank You

    Quote Originally Posted by Padonak View Post
    Code:
    function loop(){
    var aa = document.getElementsByTagName("a"),counter=0;
    for(var i=0; i<aa.length; i++){
    if(aa[i].rel && aa[i].rel=="nofollow"){if(counter<11){aa[i].style.color="#00a3cc";counter++;}}
    else{continue;}
    } 
    }
    Thank You Dear for this code but I need some more help from You, I'm a beginner in JavaScript

    I'm doing some experiment to learn JavaScript at jsfiddle.net, now I have modified the same function to display/hide the nofollow links using the single button.

    On hide, I used the same function and set the link color to default as in the css file, but I'm sure there is an easy way to do that (To undo the effect)

    Please do the needful

    Thanks

  5. #5
    Join Date
    May 2013
    Location
    India
    Posts
    4
    forgot to include the link to that jsfiddle code, here it is http://jsfiddle.net/csshacks/dGz4d/85/

  6. #6
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,636
    try this

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>123</title>
    <style type="text/css">
    a{text-decoration:none;font-weight:bold;color:#1155CC;}
    </style>
    <script type="text/javascript">
    function loop(){
    var lnk=document.getElementsByTagName("a"),
    btn=document.getElementById("button"),
    val=btn.value;
    for(var i=0;i<lnk.length;i++){
    if(lnk[i].rel=="nofollow"){lnk[i].style.color=(val=='Display')?"#00a3cc":"#1155cc";}
    else{continue;}
    }
    btn.value=(val=='Display')?'Hide':'Display';
    }    
    </script>
    </head>
    <body>
    <input type="button" value="Display" id="button" onclick="loop()"><br />
    <a href="#" rel="nofollow">Test Link 1</a><br />
    <a href="#" rel="nofollow">Test Link 2</a><br />
    <a href="#">Test Link 3</a><br />
    <a href="#" rel="nofollow">Test Link 4</a>
    </body>
    </html>
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  7. #7
    Join Date
    May 2013
    Location
    India
    Posts
    4
    Thank You Padonak for the help, it just worked perfect and I'm sure I have to learn a lot in JavaScript to make functions like you,

    I also need your feedback on my capabilities of learning the JavaScript, based on my initial efforts and silly experiments.

    Thanks a Lot

  8. #8
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,636
    all you need is time and will to learn. i'm not a profi but there are lots of skilled programmers over here who are professionals. people who really need help (not like those who do nothing themselves but keep asking for gimmi-ready-to-use-cod-pliz-pliz) are always welcome and helped at this forum.
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

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