user721588
user721588

Reputation:

Javascript popup in html table

I have a 10 x 10 with data table that is created using html tags. If it is possible to create a onClick function to each cell? I mean, if I click on a cell, then it gives me its value in a n alert window? If yes, then how?

Upvotes: 1

Views: 2307

Answers (2)

Maged Almaweri
Maged Almaweri

Reputation: 352

A good example could be found here

HTML CODE: 

<div id='page-wrap'>

Your content goes here.

<a id='show' href='#'>show overlay</a>


JavaScript & JQuery:

$(document).ready(function() {

$("#show").click(function() {
    showPopup();
});

$("#popup").click(function() {

    $(this).hide();
    $("#mask").hide();

});

});

function showPopup() {

   // show pop-up

$("#mask").fadeTo(500, 0.25);

// show the popup
$("#popup").show();
}



 ---------------------------------------


 CSS CODE: 


* { margin: 0, padding 0}

#mask{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: #000;
display: none;
z-index: 10000;

}

#popup
{
width: 200px;
height: 200px;
margin: 10px auto; 
border: 1px solid #333;
background-color: #ffffdd;
cursor: pointer;
padding: 10px;
position: relative;
z-index: 10001;
display: none;
}


  [![enter image description here][1]][1]

Upvotes: 0

mplungjan
mplungjan

Reputation: 178215

Plain JS - assuming <table id="table1">:

window.onload=function() {
  var cells = document.getElementById('table1').getElementsByTagName('td');
  for (var i=0, n=cells.length;i<n;i++) {
    cells[i].onclick=function() { alert(this.innerHTML) }
  }
}

Upvotes: 4

Related Questions