Reputation: 273
How do I get the contents of a div using Jquery.
Here is my HTML:
<div class="DataRow" data-vendorid="67">
<div class="DataColumn" style="width: 60%;">
Barracuda
</div>
<div class="DataColumn" style="width: 20%; text-align: center;">
1
</div>
<div class="DataColumn" style="width: 20%; text-align: center;">
2
</div>
</div>
Here is my Jquery:
$(function () {
$('.DataRow').click(function () {
// get value: Barracuda
});
});
Upvotes: 2
Views: 1052
Reputation: 1631
If you need to get content of the clicked div it might help you.
$(function () {
$('.DataRow').click(function (event) {
alert(event.target.innerHTML);
});
});
If you need to get only the first value:
$('.DataRow').click(function () {
var first = $(this).children().eq(0).html();
alert(first);
});
Upvotes: 3
Reputation: 3591
If you need to get only "Baracuda", i.e. first div text, you could use innerHTML like this:
$(function () {
$('.DataRow').click(function () {
// get value: Barracuda
alert($(this).children("div")[0].innerHTML);
});
});
Here is an example.
Upvotes: 1
Reputation: 145458
To get the contents of the first child:
$('.DataRow').click(function () {
var first = $(this).children(":first").html();
});
To get the contents of the third child:
$('.DataRow').click(function () {
var third = $(this).children(":eq(2)").html();
});
Upvotes: 0
Reputation: 35407
$(function () {
$('.DataRow').click(function () {
alert($(this).find(':first-child').text());
});
});
Or:
$(function () {
$('.DataRow').click(function () {
alert($(this).children().first().text());
});
});
Upvotes: 4