Solved

JQuery Call Funtions Multiple Times Onload Using Dynamic Parameters?

Posted on 2014-04-03
3
417 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

749 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