Solved

JQuery Call Funtions Multiple Times Onload Using Dynamic Parameters?

Posted on 2014-04-03
3
410 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
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 how to dynamically set the form action using jQuery.
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)

920 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

16 Experts available now in Live!

Get 1:1 Help Now