-1

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

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Noob Player
  • 279
  • 6
  • 25

4 Answers4

0

First of all, you cannot have multiple elements with same ID on a page. Change departure_time and arrival_time to classes.

Then you may add a parameter to your event, like this:

<div class="divTableRow" onclick="hey(this);">

The this keyword is an HTML element that triggered this event. In your javascript you can use it this way:

function hey(element) {
    var departure = element.getElementsByClassName("departure_time")[0].innerText;
    var arrival = element.getElementsByClassName("arrival_time")[0].innerText;
    alert(departure);
    alert(arrival);
}

And the whole snippet might look like this:

function hey(element) {
    var departure = element.getElementsByClassName("departure_time")[0].innerText;
    var arrival = element.getElementsByClassName("arrival_time")[0].innerText;
    alert(departure);
    alert(arrival);
}
.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;
    }
<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(this);">
      <div class="divTableCell1 departure_time">1200</div> 
      <div class="divTableCell1 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(this);">
      <div class="divTableCell1 departure_time">8888</div> 
      <div class="divTableCell1 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>
Marcin Pevik
  • 163
  • 1
  • 14
0

As you added tag jQuery, you can use the following snippet to solve your issue:

$("div.divTableRow").click(function(){
  var departure = $(this).find("#departure_time").text();
  alert(departure);
  var arrival = $(this).find("#arrival_time").text(); 
  alert(arrival);
});

DEMO

Kiran
  • 20,167
  • 11
  • 67
  • 99
0

Try using jQuerys click handler, so you can reference the clicked element within the click handler. The idea is to limit the scope ("#departure_time WITHIN the clicked row"). The answer, of course, is only valid as long as "filled by web service response" is true. Normally, you would use classes for

https://api.jquery.com/click/

$('.divTableRow').click(hey);

function hey (evt) {
  alert("sdsdsd");
  var departure = $(this).find('#departure_time').text();
  alert(departure);
  var arrival = $(this).find('#arrival_time').text();
  alert(arrival);
}
.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;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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">
      <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">
      <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>
SVSchmidt
  • 6,269
  • 2
  • 26
  • 37
  • You still shouldnt have multiple ID's. You could easily craft a jQuery solution which uses class names. – Jamiec May 16 '17 at 07:59
  • @Jamiec That's why I quoted the OPs sentence about getting the data from a web service with that bogus multiple IDs. – SVSchmidt May 16 '17 at 08:01
0

Do not use the same ID multiple times, change thm to classes. Thereafter if you want to do this with vanilla javascript use pass a reference to the row in your method call hey and use getElementsByClassName:

window.hey = function(div){
  var departure = div.getElementsByClassName("departure_time")[0].innerHTML;
  alert(departure);
  var arrival = div.getElementsByClassName("arrival_time")[0].innerHTML;
  alert(arrival);  
};
.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;
}
<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(this);">
      <div class="divTableCell1 departure_time">1200</div> 
      <div class="divTableCell1 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(this)">
      <div class="divTableCell1 departure_time">8888</div> 
      <div class="divTableCell1 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>
Jamiec
  • 133,658
  • 13
  • 134
  • 193