JavaScript Apply different colors to Dynamically generated row of HTML table apply different colors in rectangle,apply different colors,how to

I've created dynamic table using HTML and javascript but rather than applying alternate color, I want to apply distinct color to each row. How can I do so?

<!DOCTYPE HTML>
<html>
    <head>
        <title>Dynamic Page</title>
    </head>


    <body>
        <table>
            <tr>
                <td>Enter Rows</td>
                <td><input type="number" id="txtRows"/></td>
            </tr>
            <tr>
                <td>Enter Columns</td>
                <td><input type="number" id="txtCols"/></td>
            </tr>
            <tr>
                <td colspan="2"><input type="button" id="btnDisplay" value="Display" onClick="createTable();"/></td>
            </tr>
        </table>
        <table id="tbl_DynamicTable" border="1">
        </table>
    </body>

    <script type="text/javascript">
        function createTable()
        {
          debugger;
            var rows = document.getElementById("txtRows").value;
            var cols = document.getElementById("txtCols").value;
            var table = document.getElementById("tbl_DynamicTable");
            var str="";
            for(var i=0;i<rows;i++)
            {
                str += "<tr id=row" + i +">";

                //r = document.getElementById('tbl_DynamicTable').getElementsByTagName('<tr>');
                //r.bgColor = colours[(i%k)/group | 0];

                //mycurrent_row.style.backgroundColor = "#EEF4EA";
                //mycurrent_row.style.backgroundColor =colours[(i%k)/group | 0];

                for(var j=0;j<cols;j++)
                {
                    if(i==0)
                    {
                        str += "<th> Header " + j + "</th>";
                    }
                    else
                    {
                        str += "<td> Row " + i + ", Cell "+ j + "</td>";
                    }
                }
                str += "</tr>";             
            }           
            table.innerHTML = str;
        }       


        $("tr").style("bgcolor","red");     
    </script>
</html>

I don't know how to use jQuery with HTML page. I'm new to this. So if possible please let me know what is needed to include in this too.

Answer:1

You have many way to create a random color.

Use these example :

JAVASCRIPT

'#'+Math.floor(Math.random()*16777215).toString(16);

jQuery

(function(m,s,c){return (c ? arguments.callee(m,s,c-1) : '#') +
  s[m.floor(m.random() * s.length)]})(Math,'0123456789ABCDEF',5)

And add this hexa random color on your TR when you generate it

HTML

<table border="1">
  <tr bgcolor="#FF0000">
                  ^ Here
    <th>Month</th>
    <th>Savings</th>
  </tr>
  <tr>
    <td>January</td>
    <td>$100</td>
  </tr>
</table> 

Other example on http://www.paulirish.com/2009/random-hex-color-code-snippets/

Answer:2

I've tried playing around with jqplot to create bar chart that can be clicked and then will redirect user to another page together with label value as url parameters but so for no luck to make a link. ...

I've tried playing around with jqplot to create bar chart that can be clicked and then will redirect user to another page together with label value as url parameters but so for no luck to make a link. ...

  1. ggplot make graph bigger
  2. ggplot make line graph
  3. make bar graph ggplot
  4. ggplot make column graph
  5. make graph in ggplot

I was wondering what the difference is between ColdFusion's built-in AJAX functionality and using a JS framework like JQuery? It seems that ColdFusion ships with tags that can do fancy Javascript ...

I was wondering what the difference is between ColdFusion's built-in AJAX functionality and using a JS framework like JQuery? It seems that ColdFusion ships with tags that can do fancy Javascript ...

Please help me to adjust an existing script to replace COMMA with DOT. I use a script which limit the inserting character into Text fields. Only 1,2,3,4,5,6,7,8,9,0 and "." and "," are accepted to be ...

Please help me to adjust an existing script to replace COMMA with DOT. I use a script which limit the inserting character into Text fields. Only 1,2,3,4,5,6,7,8,9,0 and "." and "," are accepted to be ...

I'm starter. I have an idea. I want to implement an event like this. Is it possible? <html> <head> <title>First visit event</title> <script> function do_something() { ...

I'm starter. I have an idea. I want to implement an event like this. Is it possible? <html> <head> <title>First visit event</title> <script> function do_something() { ...