You are here:

Javascript/javascript code not working


QUESTION: Hello Kevin,

I have a small javascript code which i have written as a function in a different file with .js extention and i m trying to invoke the function on click of a button from a php file.Following is a snippet:

in php file under head:
<script type="text/javascript" src="add_ele.js">

in php file under form:
<button id="addphn" title="Add Phone Numbers" name="addphn" value="1" onclick="addphn()">+</button>

The problem is that when i try to call an alert function from the same script it works... but another alert function which lies inside my function does not respond to the call.

following is the javascript:
alert("i m outside the func...");

function addphn()
  alert("i m in the func now...");
  var div=document.getElementById('phn');
  var btn=document.getElementById('addphn');
  var cnt=(document.getElementById('addphn').value -1) + 2;
  //btn.value=btnval + 1;
  var newinp=document.createElement('input');
  var inpname='phn'+cnt;

plz help as i m stuck and this issue comes up each time i try to learn anything with javascript.


ANSWER: Hi Ankit,

Thanks for your question.

From what I can see, the main problem was your inline javascript. In the email I received when your question was submitted, the inline JS for your button was: _onclick="addphn()"

I removed the underscore, and chnaged that to: onclick="addphn()". When I did that, it worked fine. Also, I highly recommend that you use Unobtrusive JavaScript. Attaching your event handlers inline is a practice that you might want to stay away from.

Below are two examples. In #1, I got your code to work as-is. In example # 2,  I placed your JavaScript at the bottom of the page (also highly recommended), and used Unobtrusive JavaScript to setup the event handler.

Example # 1:

Example # 2:

note: I commented out all of JS after the alert in your function because what you are trying to do there is out of the scope of this question.

I hope this was helpful. Please feel free to follow-up if there are any other questions you may  have.

Best Wishes,

Kevin Chisholm

---------- FOLLOW-UP ----------


thanks a lot for your response and advice on unobtrusive usage of JavaScript. I read and tried running your code and mine that you had corrected. The page still shows no response. I tried checking it on different browsers but the result was same. Has it anything to do with the configurations in apache server because the code sent me is working perfectly fine in jsFiddle.

One more thing Kevin, I couldn't understand the relevance of:
         var link = document.getElementById('addphn');

Kindly enlighten. Also please provide me more tips and advices as i m a total newbie and want to learn the right thing.

Thanks a ton,

Hi Ankit,

Thanks for your follow-up question.

Your problem has nothing to do with Apache. Please re-post your question with all relevant code (i.e. attache any HTML or JavaScript to the question that I will need to understand what you are doing and be able to pinpoint the problem) .  I can't help you find a problem if I cannot see the code.

Regarding:  var link = document.getElementById('addphn');

In this line, I create a variable named "link", and assign it to the element that has the ID: "addphn"

I then reference that variable to use the addEventListener() / attachEventListener() methods.

Regarding "more tips and advices" Please re-post as a specific question. If you need assistance that cannot be put in the form of a question, please contact me directly at: and I will provide the best advice that I can. In the meantime, here are a few links that are good to start with:

I hope this was helpful. Please feel free to follow-up if there are any other questions you may  have.

Best Wishes,

Kevin Chisholm


All Answers

Ask Experts


Kevin Chisholm


I can answer questions about: JavaScript, Object-Oriented JavaScipt, JSON, AJAX, jQuery, HTML, CSS


I am a Front-End Web Developer living in New York City. I have provided services to titles such as,,,,,, and Being particularly passionate about Object-Oriented JavaScript as well as Web Standards, I spend a fair amount of time writing about / teaching Front-End Web Development techniques. In my free time I enjoy reading about advanced JavaScript, as well as Web Development trends and best practices. My Blog is: JavaScript-specific blog posts can be found here:

NYC Front-end Developers CSS3 / HTML5 The future of front end Front-End Engineers Javascript Syracuse University Alumni Network

Syracuse University - VPA - 1983 - 1987

Past/Present Clients

©2016 All rights reserved.