faruk mahir
faruk mahir

Reputation: 39

Unable to call javascript function from external.js file

I am unable to call my javascript function named as clear which takes a string parameter the parameter value im passing by calling that function on client click. here is my code please do help me in this

<script type="text/javascript">       
    var _whichform = 'login'
    function clear(_whichform)
    {

        switch (_whichform)
        {
        case 'login':
               document.getElementById('<%=tbxUsername.ClientID%>').value = ''
               document.getElementById('<%=tbxPassword.ClientID%>').value = ''
               document.getElementById('<%=tbxFname.ClientID%>').value = ''
               document.getElementById('<%=tbxLname.ClientID%>').value = ''
               break;
          break;
         case 'company':
               document.getElementById('<%=tbxCompname.ClientID%>').value = ''
               document.getElementById('<%=tbxDescptn.ClientID%>').value = ''
               document.getElementById('<%=tbxCompanyEmail.ClientID%>').value = ''
               document.getElementById('<%=tbxStateCo.ClientID%>').value = ''
               document.getElementById('<%=tbxStateCo.ClientID%>').value = ''
               document.getElementById('<%=tbxCity.ClientID%>').value = ''
               document.getElementById('<%=tbxCompanyAddress.ClientID%>').value = ''
               document.getElementById('<%=tbxCpCountryCode.ClientID%>').value = ''
               document.getElementById('<%=tbxCpStateCode.ClientID%>').value = ''
               document.getElementById('<%=tbxPhone.ClientID%>').value = ''
               document.getElementById('<%=tbxCfCountryCode.ClientID%>').value = ''
               document.getElementById('<%=tbxCfStateCode.ClientID%>').value = ''
               document.getElementById('<%=tbxCffax.ClientID%>').value = ''
          break;

        }
    }
</script>
<input id="btnLoginCancel" type="button" value="Clear" onclick="clear('login')" class="buttons"/>

Upvotes: 0

Views: 1273

Answers (4)

fiddur
fiddur

Reputation: 1877

clear() is a native function in the browser. Name your function e.g. myclear, and it will work (provided the field with the id's named are correct).

clear is a method on window.document. In SOME browsers (e.g. chromium), the onclick will be executed in that context. This can be seen e.g. by adding onclick="console.log(clear)", and you will get something like: function clear() { [native code] }

Then it's always a good idea to end all statements with semicolon, to avoid later mixups with newlines etc.

Upvotes: 0

Vignesh Subramanian
Vignesh Subramanian

Reputation: 7289

You have missed a semicolon in the second line it should be var _whichform = 'login'; Also for testing and debugging its always better to use IE since it would have shown that you have missed the semicolon in your second line.

Upvotes: 0

Ajay_Dhama
Ajay_Dhama

Reputation: 13

There are few points needs your attention.

1. If this is the external JS file then you write it wrong, there is no need of writing <script type="text/javascript"></script> tags in external js file it throws the Syntax error. you can directly start from writing a function and be careful in calling js file (path etc.)

2. You missed semicolons at several places in your js file and there is no need of defining Var _whichform because you are already passing the parameter at the time of calling the function.

3. From Developers point of view it’s better to test your code on Firefox using firebug It certainly helps in debugging and writing better javascript code.

Upvotes: 0

martriay
martriay

Reputation: 5742

You missed a semicolon.

var _whichform = 'login';

Upvotes: 2

Related Questions