Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Feb 2004
    Location
    Nagoya, Japan
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    put links inside of a function?

    inside of a javascript function, i.e.,inside of a "while", can i put somehow a button or a link that changes according with some variable inside the loop?

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,946
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Could you be more specific?

  • #3
    New Coder
    Join Date
    Feb 2004
    Location
    Nagoya, Japan
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    for example,


    function sample(array)
    {
    var i = 0;
    while (i < array.length)
    {
    document.write("Value: " + array[i] );
    <a href="" onClick="newfunction(array[i]);"> <br> dosomething </a>'
    --->> How can I write this line in a JS function?

    i++;
    }
    }

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,946
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Do you want to modify an existing link or create a new one on the fly? When do you want to create a link? As the page loads or after some events like onlick? More details please...

  • #5
    New Coder
    Join Date
    Feb 2004
    Location
    Nagoya, Japan
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I want to create buttons on the fly, one button for every value in the array, and display them all at the same time on a new page.

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,946
    Thanks
    0
    Thanked 236 Times in 233 Posts
    like this?

    Code:
    <html>
    <head>
    <script type="text/javascript">
    var arr = new Array("item1","item2","item3");
    function createLinks(){
      var str='';
      for (var i=0;i<arr.length;i++){
        str+='<a href="#" onclick="newfunction(\\''+arr[i]+'\\');return false">do something</a><br>';
      }
      document.getElementById('linkContainer').innerHTML=str;
    }
    
    function newfunction(str){
      alert(str)
    }
    </script>
    </head>
    <body>
    <form>
    <input type="button" value="Create Links" onclick="createLinks()">
    <div id="linkContainer"></div>
    </form>
    </body>
    </html>
    If this isn't what you're looking for, please describe in more details.

  • #7
    New Coder
    Join Date
    Feb 2004
    Location
    Nagoya, Japan
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks. I think that is what I wanted.

    I'm trying to apply it, but what's wrong whith this code?

    <script language = "JavaScript">
    var array = new Array(1,2,3,4,5);
    var result = new Array(1,3);

    function show_Results(result)
    {
    var str='';
    for (var i = 0;i < result.length;i++)
    {
    document.write("<br>" + "Index position: " + result[i]);
    document.write("<br>" + array[result[i]].info + "<br>");
    str+='<a href="#" onclick="newfunction(\''+result[i]+'\');return false">do something</a><br>';
    }
    document.getElementById('linkContainer').innerHTML=str;
    }

    function newfunction(str)
    {
    alert("");
    }

    </script>
    <input type="button" value="Test" onclick="show_Results(result)">
    <div id="linkContainer"></div>

    -----

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,946
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Remove the 2 document.write statements as these will overwrite the whole page. If you really need them and not for debugging purposes only, then the code should be like this:

    Code:
    var array = new Array(1,2,3,4,5);
    var result = new Array(1,3);
    
    function show_Results(result)
    {
    var str='';
    for (var i = 0;i < result.length;i++)
    {
    str+='<br>Index position: ' + result[i];
    str+='<br>' + array[result[i]] + '<br>'; 
    str+='<a href="#" onclick="newfunction('+result[i]+');return false">do something</a><br>';
    }
    document.getElementById('linkContainer').innerHTML=str;
    }
    
    function newfunction(num)
    {
    alert(num);
    }
    I removed the single quote in the newfunction parameter as I noticed you're passing an integer not string.
    And what's with array[result[ i ]].info? The variable array is an array of numbers not array of objects.

  • #9
    New Coder
    Join Date
    Feb 2004
    Location
    Nagoya, Japan
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    and how should I do to make it without having a button that calls the function? That is, I want the function to run inmediately ater the page is loaded.

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,946
    Thanks
    0
    Thanked 236 Times in 233 Posts

    onload

    <body onload="show_Results(result)">

    or inside the script tag:

    window.onload=function(){show_Results(result)};


  •  

    Posting Permissions

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