Solved

JQuery Call Funtions Multiple Times Onload Using Dynamic Parameters?

Posted on 2014-04-03
3
406 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
Comment Utility
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
Comment Utility
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
Comment Utility
thx
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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 …
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now