Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.