Obtain the value of the class when click()

javascript get class of clicked element
jquery get clicked element
onclick find class jquery
jquery get value of clicked element
get class in this jquery
jquery click event on class name
jquery get class attribute
jquery check if class clicked

I'm using jQuery to activate a function every time the user clicks on a button that has a certain class. I'm trying to get the value of the button clicked, and so far, failing.

window.onload = function() {
    $('.button').click(function(item) {
        console.log(item.value);
    });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="button" value="7">7</button>

basic one...

Pure jQuery:

$(document).ready(function() {
  $('.button').click(function(item) {
    console.clear();
    console.log( $(this).text(), ' = ', $(this).val() );
  });
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="button" value="7">bt 7</button>
<button class="button" value="8">bt 8</button>

How to get the class of the clicked element?, I can't figure it out how to get the class value of the clicked element. Here's a quick jQuery example that adds a click event to each "li" tag, and then retrieves the class attribute for the clicked element. Hope it helps. And in newer browsers you can get the full list of class names: Documentation: .on(). To reffer to the clicked element, you use the $ (this) instruction, so, the syntax to get the value of an attribute of a clicked HTML element is: $ (element).click (function () { var attrval = $ (this).attr ('attribute_name'); }); Now, here's some code, and examples.


window.onload = function() {
    $('.button').click(function(e) {
        console.log(e.target.textContent);
    });
}

jQuery: Find the class of the clicked element, click(function() { var colorClass = this.className; console.log(colorClass); });. Haml; JS. Result. Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.


If your want to stick with using just JQuery, Use $(this).val() That should get you the value of the button.

change your JS to this:

$(document).ready (function() {
    $('.button').click(function(item) {
        console.log($(this).val());
    });
});

Try out this Jsfiddle as well to see if this is what you're looking for: https://jsfiddle.net/9zjLfso8/

Also if you're going to use jQuery, I would recommend that you use $(document).ready instead of window.onload. Here's a link to see the difference between the two: window.onload vs $(document).ready()

Get the value of the name attribute of each element with class="class , $("#Button").click(function() { var items = document.getElementsByClassName('​class'); for (var i = 0; i < items.length; i++) alert(items[i].name); });  I'd -2 this if I could, once for $(this).attr('id') and another for replacing a perfectly good class selector (.vote) with a fragile check for a very specific class value that will break if the element ever gets two classes. – Alnitak Aug 7 '14 at 17:19


You can do it with vanilla JavaScript:

   <button class="button" 
   onclick="myFunc(7)">7</button>

The script:

   function myFunc(value){
       console.log(value)
   }

getting value of a clicked element, If I have an img tag with id="blah" and value="x" I know can get the value use a .click() event to get the id of clicked element then get its value by id? You can use selectors for CSS class, name, attribute values, etc. etc. etc. In this post, I will tell you, How to get input field value on button click in reactjs? Reactjs is a Javascript Library to build user interface. In this post, I made simple form and input field and input button. On button click, I am getting the input box value and this is tricky and in future post, I will do more this form.


How to get the closest input value from clicked element with jQuery?, Now, find the input with the given name using the attribute equals selector. The last step is to use val() metjod to retrieve the value of the input. Get Value or Selected Option in Select Box. There are a variety of ways to use JavaScript to obtain the value of the selected option in a select list or a reference to the selected option: Use the value property of the select list. Use the selectedIndex property.


jQuery Get Content and Attributes, The jQuery attr() method is used to get attribute values. The following example demonstrates how to get the value of the href attribute in a link: Example. $("​button")  We have find all radio button object using class selector, I have passed '.getvalue' class name into the jQuery selector. Then, found all the checked radio element and alert value. Option 2: Get The Value Of Selected Radio Button Using jQuery


jQuery toggleClass() Method, The toggleClass() method toggles between adding and removing one or more A Boolean value specifying if the class should only be added (true), or only be  Click the checkbox given above or click each checkbox. You will get a single selection only and get single value on each selection. Get Multiple Selected Checkbox Value Using jQuery. In addition to the above example, you can also get value for multiple selections. To get value for the multiple selections, you