Jonathan Blaine
Jonathan Blaine

Reputation: 65

Pass jQuery variable to Javascript Function

I have a table in a separate HTML file that I am loading with jQuery. I am then defining the variable "aa". I am attempting to use this variable in my JavaScript Function "report(period)". I tried creating a global variable but that didn't help. I am not entire sure I was doing it correctly. I am fairly new to JavaScript and know even less of jQuery. I've gone through other similar posts but it’s very difficult to understand exactly what's happening. Any help would greatly be appreciated.

jQuery

jQuery(function($) {
    aa = document.getElementById('part1Table').rows[0].cells[2].innerHTML;
});

Javascript

function report(period) {   

    x = document.getElementById("tblabiNew").rows[2].cells[1].innerHTML; /*----- for testing use a number instead (example: x = "205-000040-634") ------*/

/*---------------------------------------------------------------------------------------------- Start - Object Removal Control ------------------------------------------------------------------------------------*/

    if (x==aa)  {

        var i = 1;  do {
            + i; i++; 
            var e =  document.getElementById (i);  
            e.style.display = 'none'
        } while (i < 15)

        /*polebrea21*/
        var polebrea = 21;  
        do {
            + polebrea; 
            polebrea++; 
            var e =  document.getElementById (polebrea);  
            e.style.display = 'none'
        } while (polebrea < 28)

        /*polebrea31*/
        var polebrea = 31;  
        do {
            + polebrea; 
            polebrea++; 
            var e =  document.getElementById (polebrea);  
            e.style.display = 'none'
        } while (polebrea < 38)

        /*regulatory51*/
        var regulatory = 51;  
        do {
            + regulatory; 
            regulatory++; 
            var e = document.getElementById (regulatory);  
            e.style.display = 'none'
        } while (regulatory < 64)
        /*regulatory51*/

        /*regulatory81*/
        var regulatory = 81;  
        do {
            + regulatory; 
            regulatory++; 
            var e = document.getElementById (regulatory);  
            e.style.display = 'none'
        } while (regulatory < 94)
   }; 
};

Upvotes: 1

Views: 6886

Answers (2)

Damian Polac
Damian Polac

Reputation: 934

If you want "global" variable you should declare it outside of all functions body. So this should be.

var aa;
jQuery(function($) {
    aa = //do something with aa
});

but anything you use without declaring is by default global (mind it work that way only in browsers).

If you want create local variable, add var keyword before it name, like this:

function report(period) {   

    var x = //...

}

I believe your aa variable is not declared because report function is called before page is ready.

Everything in function given to jQuery() run after DOM is ready, so if I write:

jQuery(function($) { console.log(1); });
console.log(2);

I get "2, 1" and not "1, 2".

You should really learn JavaScript and jQuery if you want to use it. Your report code seems like it can be replaced with one line with jQuery.

Upvotes: 1

lorefnon
lorefnon

Reputation: 13095

If I understand your scenario correctly you are not able to obtain the relevant node because the HTML fetched via ajax has not been injected into the DOM and hence can't be fetched using document.getElementById.

Could you provide the code which fetches the remove HTML and then what is done with it ? That may be helpful to understand the situation.

Anyway, this is something you may want to try:

$.ajax({
  method: "GET",
  url: "some/remote/url",
  success: function(htmlContent) {
    aa = $(htmlContent).find('#part1Table')[0].rows[0].cells[2].innerHTML;
    // Do some processing
  }
})

Upvotes: 0

Related Questions