How to get the html content of a clicked element using jQuery

Olawale Akinseye

I am trying to get the inner html content of a particularly clicked div, I have a number of div elements.

Look at the code I use below.

$(function() {

    $( "#try" ).click(function() {
        var clickedValue = $(this).find('div').text();
        alert(clickedValue);
    });

});
<div id="try">
    <div class="cchoice" style="background-color: blue;">B</div> 
    <div class="cchoice" style="background-color: red;">R</div> 
    <div class="cchoice" style="background-color: yellow;">Y</div> 
    <div class="cchoice" style="background-color: black;">B</div> 
    <div class="cchoice" style="background-color: gray;">G</div> 
    <div class="cchoice" style="background-color: white;">W</div> 
</div>

I want to do it in a way such that B appears when the div with background color of blue is clicked on but then it seems something is wrong with the way I am doing it. For some reasons, I cannot give id attribute to those set of div and they must have the same class.

The problem with the code above is that when I click on any of the 6 div elements, the clickedvalue will be BRYBGW.

Arun P Johny

$(this).find('div') will return all the div elements within the #try element, instead you want to target only the clicked div

You can use event.target

$(function() {
  $("#try").click(function(e) {
    var clickedValue = $(e.target).text();
    alert(clickedValue);
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="try">
  <div class="cchoice" style="background-color: blue;">B</div>
  <div class="cchoice" style="background-color: red;">R</div>
  <div class="cchoice" style="background-color: yellow;">Y</div>
  <div class="cchoice" style="background-color: black;">B</div>
  <div class="cchoice" style="background-color: gray;">G</div>
  <div class="cchoice" style="background-color: white;">W</div>
</div>


But I would recommend targeting the actual child element using the click handler.... either by directly binding the handler to them or by using event delegation

$(function() {
  //or $("#try > div").click(function(e) {
  $("#try").on('click', 'div', function(e) {
    var clickedValue = $(this).html();
    alert(clickedValue);
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="try">
  <div class="cchoice" style="background-color: blue;">B</div>
  <div class="cchoice" style="background-color: red;">R</div>
  <div class="cchoice" style="background-color: yellow;">Y</div>
  <div class="cchoice" style="background-color: black;">B</div>
  <div class="cchoice" style="background-color: gray;">G</div>
  <div class="cchoice" style="background-color: white;">W</div>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the id or class an HTML element clicked?using Jquery

From Dev

How to display only clicked element in HTML using JQUERY

From Dev

How to get html element id using jquery

From Dev

Get id of clicked element using jquery

From Dev

How to get all html content of the clicked control?

From Dev

How to get information from a clicked element with JQuery?

From Dev

How to get parent element to grow with content using JQuery

From Dev

Get element content in angular (alternative to .html() in jQuery)

From Dev

Get element content in angular (alternative to .html() in jQuery)

From Dev

Get content of a <title> element in html using JavaScript

From Dev

Get the text content of an html element using xpath

From Dev

Get content of a <title> element in html using JavaScript

From Dev

How to get the the clicked values dynamically using jquery?

From Dev

How to get text of element that couple levels above from link that was clicked using jQuery?

From Dev

How to get html text from a relative element using JQuery?

From Dev

Get position of clicked element with jquery?

From Dev

how to get content of an element with HTML nodes?

From Dev

How to get a HTML element by searching the content of innerHTML?

From Dev

Get Html Content from xml using jquery

From Dev

How to load the content of <h> element using jquery

From Dev

How can I get in 'clicked' element in jquery ajax success callback

From Dev

How to get the id value of an html <li> element added to an html <ul> element using prepend() method in jQuery ?

From Dev

How to get the id value of an html <li> element added to an html <ul> element using prepend() method in jQuery ?

From Dev

How to get class value of a clicked element and use this value to put a piece of html code after the last occurance of this class in jquery

From Dev

How to get class value of a clicked element and use this value to put a piece of html code after the last occurance of this class in jquery

From Dev

get a specific ancestor of a html element using jQuery

From Dev

How to get clicked element from the DOM using AngularJS?

From Dev

How to get clicked element from the DOM using AngularJS?

From Dev

How to get the index of the clicked array element using JavaScript?

Related Related

  1. 1

    How to get the id or class an HTML element clicked?using Jquery

  2. 2

    How to display only clicked element in HTML using JQUERY

  3. 3

    How to get html element id using jquery

  4. 4

    Get id of clicked element using jquery

  5. 5

    How to get all html content of the clicked control?

  6. 6

    How to get information from a clicked element with JQuery?

  7. 7

    How to get parent element to grow with content using JQuery

  8. 8

    Get element content in angular (alternative to .html() in jQuery)

  9. 9

    Get element content in angular (alternative to .html() in jQuery)

  10. 10

    Get content of a <title> element in html using JavaScript

  11. 11

    Get the text content of an html element using xpath

  12. 12

    Get content of a <title> element in html using JavaScript

  13. 13

    How to get the the clicked values dynamically using jquery?

  14. 14

    How to get text of element that couple levels above from link that was clicked using jQuery?

  15. 15

    How to get html text from a relative element using JQuery?

  16. 16

    Get position of clicked element with jquery?

  17. 17

    how to get content of an element with HTML nodes?

  18. 18

    How to get a HTML element by searching the content of innerHTML?

  19. 19

    Get Html Content from xml using jquery

  20. 20

    How to load the content of <h> element using jquery

  21. 21

    How can I get in 'clicked' element in jquery ajax success callback

  22. 22

    How to get the id value of an html <li> element added to an html <ul> element using prepend() method in jQuery ?

  23. 23

    How to get the id value of an html <li> element added to an html <ul> element using prepend() method in jQuery ?

  24. 24

    How to get class value of a clicked element and use this value to put a piece of html code after the last occurance of this class in jquery

  25. 25

    How to get class value of a clicked element and use this value to put a piece of html code after the last occurance of this class in jquery

  26. 26

    get a specific ancestor of a html element using jQuery

  27. 27

    How to get clicked element from the DOM using AngularJS?

  28. 28

    How to get clicked element from the DOM using AngularJS?

  29. 29

    How to get the index of the clicked array element using JavaScript?

HotTag

Archive