Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

JQuery Call Funtions Multiple Times Onload Using Dynamic Parameters?

Posted on 2014-04-03
3
Medium Priority
?
429 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
Suggested Courses

927 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