qtt qtt
qtt qtt

Reputation: 187

Alert popup onclick not working

FIXED: I found out why: it is working fine now. I just had to use chrome manually rather than use the Brackets live viewer.

I would like the website to popup an alert box when the "Home" is clicked. I have tried to create a separate javascript file and linking it in my HTML. I read up about this on other questions, and I followed some answers and came up with this. However, when I click the "Home" nothing happens. Please help me fix this issue.

My code is below:

Here is my HTML:

function popup() {
  alert("I am a pop up ! ");
}
body {
  background-color: #efefef;
  margin: 0 auto;
  font-family: sans-serif;
}

.nav_bar {
  background-color: #cccccc;
  border: 1px solid #000000;
  width: 900px;
  margin: auto;
  font-size: 22px;
  font-weight: bold;
  text-transform: uppercase;
  padding: 10px 20px 10px 20px;
  -webkit-border-radius: 10px;
  -moz-border-radius: 10px;
  border-radius: 10px;
  text-align: center;
  margin-top: 10px;
}

.nav_bar a {
  color: #000000;
  text-decoration: none;
  margin-left: 10px;
  margin-right: 20px;
}

.nav_bar a:hover {
  color: darkblue;
  text-decoration: underline;
}
<div class="nav_bar">


  <a href="#" onclick="popup(); return false;">Home</a>
  <a href="#">About</a>
  <a href="#">Google</a>

</div>


<h1>Test website</h1>
<h4>Version 1.0</h4>

Upvotes: 1

Views: 1510

Answers (1)

napi15
napi15

Reputation: 2402

In the html :

 <a href='#' id='mylink'>click me</a>

In your JS

var myLink = document.getElementById('mylink');

myLink.onclick = function(){

      alert("I am a pop up ! ");

}

EDIT :

Make sure your JS file is linked properly in the correct folder path . May be this could help :

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

Upvotes: 1

Related Questions