Solved

JQuery Call Funtions Multiple Times Onload Using Dynamic Parameters?

Posted on 2014-04-03
3
414 Views
Last Modified: 2014-04-03
I have a page with many HTML tables. Each table has a dynamic id (tbl_1, tbl_2, etc...). I need JQuery code to call the following function multiple times to compare multiple tables placed  side-by-side. Then compare tbl_1 <- tbl_2, tbl_2 <- tbl_3, etc...

 $(function () //onload
    {
        // This is where I get stuck on iterating the functions
        // int id = 1
        // Call the functions below
        // id++

        $('#tbl_1 tr').each(function ()
        {
            var leftTableCell = $(this);

            $('#tbl_2 tr').each(function ()
            {
                if ($(this).html() === leftTableCell.html())
                {
                    $(this).css('background-color', 'yellow')
                }    
            });
        });
    });
    </script>

Open in new window

0
Comment
Question by:WorknHardr
  • 2
3 Comments
 
LVL 22

Accepted Solution

by:
Mrunal earned 500 total points
ID: 39975662
You can use for loop starting from 1 to maximum as table tag's count on page/container.
and update table id dynamically adding for loop variable.

like:
var totalTables = $(table).length;

for(var counter = 1; counter < totalTables - 1; counter++)
{

$('#tbl_' + counter + ' tr').each(function ()
        {
            var leftTableCell = $(this);

            $('#tbl_' + (counter + 1) + ' tr').each(function ()
            {
                if ($(this).html() === leftTableCell.html())
                {
                    $(this).css('background-color', 'yellow')
                }    
            });
        });
}

Open in new window


Hope this logic helps you.
0
 

Author Comment

by:WorknHardr
ID: 39975743
Cool!

var totalTables = $(table).length;

Q.  $(table).length ?

Q. What happens if the for-loop calls tbl_15 and it doesn't exist?
0
 

Author Closing Comment

by:WorknHardr
ID: 39976901
thx
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question