Jump to content

function won't pass value


EchoFool

Recommended Posts

Hey

 

I have a JS value but it won't put the value in the brackets when i try to get the element by ID instead it is looking for the id named the same as the variable name.

 

Here is what i got to explain better:

 

function fill_div(v) 
    { 
        document.getElementById(v).innerHTML="<img src='untitle.png' style='width:100%;height:100%;'/>";
    }
var x = 0;
var y = 3;
var area = 1;
window.onload = fill_div(area+':'+x+':'+y);

 

It ends up looking for id v instead of id 1:0:3.

 

 

Any idea how i correct this ?

Link to comment
https://forums.phpfreaks.com/topic/222162-function-wont-pass-value/
Share on other sites

window.onload = fill_div(area+':'+x+':'+y);

What that says is to execute fill_div (given whatever arguments) and set the result to be window.onload. It does not say that window.onload should execute fill_div (given whatever arguments).

When this script is executing, likely the 1:0:3 (which is a horrible ID for an element) element doesn't exist yet.

 

window.onload = function() {
    fill_div("1:0:3");
};

Tip: learn to add listeners, not to override event handlers.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.