krishna
krishna

Reputation: 11

Object expected error message in Javascript

<script type="application/javascript" language="javascript">
    function showElement(elementID, show){
    var element = document.getElementById(elementID);
    if (element) {
      element.className = (show ? element.className.replace(/hidden/gi, "show") : element.className + " hidden");
     }
    }
</script>
        
        <table cellpadding="3" cellspacing="1" border="0" width="100%">  
            <tr class="baseGrayMedium">
                <td colspan="2">
                    (<a href="javascript:void(0);" onClick="showElement('evicChkLst',true);" class="nostyle">+</span></a>|<a href="javascript:void(0);" onClick="showElement('evicChkLst',false);" class="nostyle">-</span></a>) &nbsp;&nbsp; <B>Eviction Checklist</B>
                </td>
            </tr>
        </table>

I get the Javascript error saying object expected and it points to onClick event in the HTML code.. Could some one suggest me why so?

EDIT:

<script type="application/javascript" language="javascript">
                function showElement(elementID, show){
                    var element = document.getElementById(elementID);
                    if (element) {
                        element.className = (show ? element.className.replace(/hidden/gi, "show") : element.className + " hidden");
                    }
                }
            </script>
            
            <table cellpadding="3" cellspacing="1" border="0" width="100%">     
                <tr class="baseGrayMedium">
                    <td colspan="2">
                        (<a href="#" onclick="javascript:showElement('evicChkLst',true);" class="nostyle">+</span></a>|<a href="#" onclick="javascript:showElement('evicChkLst',false);" class="nostyle">-</span></a>) &nbsp;&nbsp; <B>Eviction Checklist</B>
                    
                    </td>
                </tr>
            </table>

Now the code looks some thing like the above

Upvotes: 1

Views: 25264

Answers (3)

Gregoire
Gregoire

Reputation: 24832

Your problem is: <script type="application/javascript" language="javascript"> It must be <script type="text/javascript" language="javascript">

Upvotes: 3

Matt
Matt

Reputation: 477

I just had this error and it was because I had a form element that was trying to submit to an action that was invalid.

Mine was and that was not a valid action.

If you are still having this error you should verify syntax as well as that your form is submitting to a valid method.

This error indicates bad html syntax or invalid source.

Upvotes: 0

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123473

I don't immediately see anything wrong in your snippet.

It's possible that formatting elsewhere in your script has messed up the definition or scope of showElement. Try adding this link next to the others:

<a href="javascript:void(0)" onclick="alert(typeof showElement);">?</a>

It should alert function if everything up to that point is good (or, at least, not alert undefined).

Upvotes: 2

Related Questions