Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I'm totally beginner in javascript. I made a form with a button but the external javascript is not working.

HTML
<head>
    <title>...</title>
    <script type="text/javascript" src="/media/js/script.js"></script>
</head>
<body>
      <form name="infoForm1" id="mainInfoForm">
             ...
             <input type="button" name="btnSaveMainInfo" value="ABC" onClick="validateAndCreate()" />
      </form>
</body>


What I have tried:

This is external javascript I'm using:

JavaScript
function validateAndCreate(){
     alert("aaabbbccc");
}
Posted
Updated 18-Apr-16 21:43pm
v2
Comments
Passion4Code 19-Apr-16 2:33am    
When will this function be called? You have no link to the function in the HTML elements.
F-ES Sitecore 19-Apr-16 4:29am    
You should learn to use the browser tools to debug these issues (F12 on IE or Chrome). First check the network tab to ensure the js file is found (ie that it doesn't have a status of 404), then check the console for errors.

Add a dot to indicate that the relative path starts from current directory, i.e.
src="./media/js/script.js"

btw, a path starting with
/

is relative to site root.
Learn more: definition: linking, absolute, relative, root, path, hyperlinks | Motive Web Design Glossary[^]
 
Share this answer
 
v3
Comments
Karthik_Mahalingam 19-Apr-16 4:48am    
5 for Learn more :)
Peter Leow 19-Apr-16 4:49am    
Thank you, Karthik.
PeMaCN 19-Apr-16 5:01am    
No,this is not working
Peter Leow 19-Apr-16 5:03am    
What is not working, be more specific. Where did you place the external js file?
PeMaCN 19-Apr-16 5:36am    
Sorry, it's working. I had one bracket which was causing javascript not working
Hello ,

Try adding the below:
HTML
<input type="button" name="btnSaveMainInfo" onclick="validateAndCreate()" value="ABC" />
   </form>


Edit fiddle - JSFiddle[^]

You need to have an onclick event attached to the input DOM for the function to be called.

Thanks
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900