views:

1027

answers:

5

I have this piece of Javascript and it just won't work. I allready checked JSlint but that said everything works. Still doesn't work. The javascript is located not in the HTML but is linked in the <head>

note: I am working with a local server, so pageload in instant.

function changeVisibility() {
    var a = document.getElementById('invisible');
    a.style.display = 'block';
}

var changed = document.getElementById('click1');
changed.onchange = changeVisibility;

This here is the corresponding HTML

<input type="file" name="click[]" size="35" id="click1" />
<div id="invisible" style="display: none;">
  <a href="javascript:addFileInput();">Attach another File</a>
</div>

So what happens is I click on the input, select a file and approve. Then then onchange event triggers and the style of my invisible div is set to block.

Problem is, I keep getting this error:

"changed is null: changed.onchange = changeVisibility;"

i don't get it, I seriously don't get what I'm overlooking here.


EDIT: question answered, thank you Mercutio for your help and everyone else too of course. Final code:

function loadEvents() {
    var changed = document.getElementById('click1');
    var a = document.getElementById('invisible');
    document.getElementById('addField').onclick = addFileInput;

    changed.onchange = function() {
     a.style.display = 'block';
    }
}
if (document.getElementById) window.onload = loadEvents;

This here is the corresponding HTML:

<input type="file" name="click[]" size="35" id="click1" />
<div id="invisible" style="display: none;">
  <a href="#">Attach another File</a>
</div>

Also, thanks for the link to JSbin, didn't know about that, looks nifty.

+6  A: 

This sounds like the DOM object doesn't exist at the time of referencing it. Perhaps change your code to execute once the document has fully loaded (or place the javascript at the bottom of your page)

note: I am working with a local server, so pageload in instant.

that's not the issue - the constituent parts of a document are loaded in order. It doesn't matter how fast they are loaded, some things happen before others :D

The onlything I'd like to do now is remove the Javascript link from the ...

Place an id on there, and inside your function do this:

document.getElementById('addField').onclick = addFileInput;

Or, as you already have the div as the variable 'a':

a.firstChild.onclick = addFileInput;

But this obviously leaves you with an invalid anchor tag. Best practice suggests that you should provide a way to do it without javascript, and override that functionality with your javascript-method if available.

mercutio
Mercutio, you're a lifesaver. My HTML is now Javascript free. Lots of thanks.
Vordreller
A: 

You need to wrap your code in a window.onload event handler, a domReady event handler (available in most modern js frameworks and libraries) or place at the bottom of the page.

Placing at the bottom of the page works fine, as you can see here.

Decoupling event responder from your markup is covered under the topic of "Unobtrusive JavaScript" and can be handled in a variety of ways. In general, you want to declare event responders in a window.onload or document.ready event.

Sasha Sklar
A: 

I think its because you are trying to modify a file element.

Browsers don't usually let you do that. If you want to show or hide them, place them inside of a div and show or hide that.

FlySwat
that's exactly what I'm doing. i have a link which is inside a div which is set to invisible. I want to make it so that when I select a file to upload, the link becomes visible.
Vordreller
I don't think that would be the case. Regardless of the permissions on file inputs, var changed = document.getElementById('click1'); is returning NULL, which means at that point in time, there's no element with id = "click1"
nickf
so basically, everytime I click something or do something in the HTML file, al values are redefined? I know how to do basic commands in Javascript, but the internal working and communication with HTML is a bit far from what I've seen uptill now, but I need the functionality of what I'm making
Vordreller
A: 

mercutio is correct. If that code is executing in the HEAD, the call to "document.getElementById('click1')" will always return null since the body hasn't been parsed yet. Perhaps you should put that logic inside of an onload event handler.

A: 

Right, I've modified things based on your collective sudgestions and it works now. Onlything bothering me is the direct reference to Javascript inside the anchor

Vordreller