The E
The E

Reputation: 747

JS function in external file not recognized

This is in IE7. It's a corporate setting, so not using IE7 is not an option. I'm stuck with it.

I have a textarea in a form:

<textarea name="details" id="details" cols="80" rows="20" onfocus="detailsPrompt('focus')">Enter New Details Here</textarea>

There is a javascript file called in the portion of the html:

<script type="text/javascript" src="askopss.js"></script>

In that javascript file is this function:

function detailsPrompt(taken) {
if ((taken === 'focus') && (document.getElementById('details').value==='Enter New Details Here')) {
document.getElementById('details').value='';
}
}

When I click in the textarea, I expect the value to clear out. Except, nothing happens and this error appears:

Line: 196 Char: 1 Error: Object expected Code: 0

line 196 corresponds to the line of HTML with the tag

I had this problem with another function that was being called "onload" and I solved that by moving the function to a different .js file and then calling that js file in its own tag at the end of the document. It's not a solution that works for an onclick event, however.

Any idea what the problem is?

edit: included suggestions below, but it still doesn't work.

Upvotes: 0

Views: 1153

Answers (1)

Quentin
Quentin

Reputation: 944530

This line:

detailsPrompt(taken) {

Should read:

function detailsPrompt(taken) {

While:

document.getElementById('details').value

won't find your textarea because the textarea doesn't have an id.

Give the textarea an id attribute with the value details

That said, using a default value of a substitute for a <label> is a nasty hack with serious accessibility implications.

Upvotes: 2

Related Questions