Announcement

Collapse
No announcement yet.

Jquery Array to li Question

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Jquery Array to li Question

    Hi

    I need help with Jquery, for educational reasons I cannot upload code as they have software that checks for plagiarism but i will explain what i have

    within my HTML i have a ul structure with 0 Li's, I also have a link which contains an array

    ["one","two","three"]

    What i need to do is add the Jquery array in to individual Li tags, again for educational reasons i would prefer to be pointed in the direction rather than the code being straight out given to me. I am currently looking up using .each within Jquery

  • #2
    This Might Help...

    ... i have a ul structure with 0 Li's ... add the ... array in to individual Li tags ..
    HTML Code:
    <!DOCTYPE html>
    <html>
      <head>
        <script>
    
    var listarray = ["one", "two", "three"];
    
    function createlist(listid, fromarray) {
        fromarray.forEach(function(value,index) {
           document.getElementById(listid).innerHTML += "<li>"+ value +"<\/li>";
        });
    };
    
        </script>
      </head>
      <body>
         
         <ul id="mylist"></ul>
    
         <script>
             createlist("mylist", listarray);
         </script>
          
      </body>
    </html>
    www.w3schools.com/jsref/jsref_forEach.asp
    www.w3schools.com/jsref/prop_html_innerhtml.asp
    Last edited by Structure; 04-15-2016, 12:14 PM.

    Comment


    • #3
      that solves the majority of my question except the var

      hypothetically say the index page of google.com contains

      ["One", "Two", "Three"]

      how can i use the url to save it as a variable

      Comment


      • #4
        This Might Help...

        use the url to save it as a variable
        HTML Code:
        <!DOCTYPE html>
        <html>
          <head>
            <script>
        
        var listarray = [];
        
        var paramstring = String(document.location.href).split("?")[1].split("#")[0];
        var params = String(paramstring).split("&");
        
        params.forEach(function(value,index) { 
            var paramname = String(value).split("=")[0];
            var paramvalue = String(value).split("=")[1];
            listarray.push(paramvalue);
        });
        
        function createlist(listid, fromarray) {
            fromarray.forEach(function(value,index) {
               document.getElementById(listid).innerHTML += "<li>"+ value +"<\/li>";
            });
        };
        
            </script>
          </head>
          <body>
             
             <ul id="mylist"></ul>
        
             <script>
                 createlist("mylist", listarray);
             </script>
              
          </body>
        </html>
        www.w3schools.com/jsref/prop_loc_href.asp
        www.w3schools.com/jsref/jsref_split.asp
        Last edited by Structure; 04-18-2016, 12:11 PM.

        Comment

        Working...
        X