I am new to JavaScript, I am trying to get the value of div on click in JavaScript. Here is my code:
HTML
<div class="divTable">
<div class="divTableHeading">
<div class="divTableRow">
<div class="divTableCell" >DEPARTURE TIME</div>
<div class="divTableCell">ARRIVAL TIME</div>
<div class="divTableCell">FARE</div>
<div class="divTableCell">BUS TYPE</div>
<div class="divTableCell">AVAILABLE SEATS</div>
<div class="divTableCell">STATUS</div>
</div>
</div>
<div class="divTableBody" id="divTableBody">
<div class="divTableRow" onclick="hey();">
<div class="divTableCell1" id="departure_time">1200</div>
<div class="divTableCell1" id="arrival_time">1615</div>
<div class="divTableCell1">1600</div>
<div class="divTableCell1">VOLVO</div>
<div class="divTableCell1">8</div>
<div class="divTableCell1">OK</div>
</div>
<div class="divTableRow" onclick="hey();">
<div class="divTableCell1" id="departure_time">8888</div>
<div class="divTableCell1" id="arrival_time">9999</div>
<div class="divTableCell1">1600</div>
<div class="divTableCell1">VOLVO</div>
<div class="divTableCell1">8</div>
<div class="divTableCell1">OK</div>
</div>
</div>
</div>
CSS
.divTable{
display: table;
width: 100%;
margin-top:20px;
}
.divTableRow {
display: table-row;
text-align: -webkit-center;
}
.divTableHeading {
background-color: rgba(0, 102, 179, 0.6) !important;
color: #fff;
display: table-header-group;
white-space: nowrap;
}
.divTableCell, .divTableHead {
border: 1px solid #e3e3e3;
display: table-cell;
padding: 3px 10px;
width:30%;
white-space: nowrap;
}
.divTableCell1, .divTableHead {
border: 1px solid #e3e3e3;
display: table-cell;
padding: 3px 10px;
white-space: nowrap;
}
.divTableHeading {
background-color: #EEE;
display: table-header-group;
font-weight: bold;
}
.divTableFoot {
background-color: #EEE;
display: table-footer-group;
font-weight: bold;
}
.divTableBody {
display: table-row-group;
}
JavaScript
function hey() {
alert("sdsdsd");
var departure = document.getElementById("departure_time").innerHTML;
alert(departure);
var arrival = document.getElementById("arrival_time").innerHTML;
alert(arrival);
}
Basically, its a table created in DIV tags, and the data shown here is from web service response. When I click on any divTableRow it only gives me the result of first row, But what I want is if user click on second row it should alert the second row data.
Here is the link to JSFiddle