Skip to content
Fix Code Error

Get the value of checked checkbox?

March 13, 2021 by Code Error
Posted By: Anonymous

So I’ve got code that looks like this:

<input class="messageCheckbox" type="checkbox" value="3" name="mailId[]">
<input class="messageCheckbox" type="checkbox" value="1" name="mailId[]">

I just need Javascript to get the value of whatever checkbox is currently checked.

EDIT: To add, there will only be ONE checked box.

Solution

For modern browsers:

var checkedValue = document.querySelector('.messageCheckbox:checked').value;

By using jQuery:

var checkedValue = $('.messageCheckbox:checked').val();

Pure javascript without jQuery:

var checkedValue = null; 
var inputElements = document.getElementsByClassName('messageCheckbox');
for(var i=0; inputElements[i]; ++i){
      if(inputElements[i].checked){
           checkedValue = inputElements[i].value;
           break;
      }
}
Answered By: Anonymous

Related Articles

  • Is CSS Turing complete?
  • Javascript validate all checkboxes are selected
  • setTimeout function not working : javascript
  • How can I require at least one checkbox be checked…
  • collision detection from picturebox in a list c#
  • Navbar not filling width of page when reduced to mobile view
  • How do I switch two players in tic-tac-toe game in Python?
  • How to add fade animation for this tab
  • What is a NullReferenceException, and how do I fix it?
  • How do i update a javascript variable as its value changes?
  • JavaScript gives NaN error on the page but variable…
  • Trying to keep dropdown menus flush to the edge of…
  • Creating a custom counter in Spark based on…
  • Combining Ember Table with Ember Data
  • Not submit any value if checkbox is empty
  • Dart core element core-collapse
  • Dart - Polymer Unit Testing. Not able to reference…
  • Active tab issue on page load HTML
  • Customize Bootstrap checkboxes
  • Switch statement for greater-than/less-than
  • Is there a better, cleaner way to write this…
  • For loop for HTMLCollection elements
  • CSS Input with width: 100% goes outside parent's bound
  • How can I pattern match ID only making sure the…
  • What are the currently supported CSS selectors…
  • Interdependent properties in Vue.js
  • Remove similar tuple from dictionary of tuples
  • Data not catching in multi step contact form
  • Vuex dynamic checkboxes binding
  • Paper checkbox and -webkit-column-count
  • Checkbox Check Event Listener
  • How do I include a JavaScript file in another…
  • What's the best way to get the last element of an…
  • How do I keep only the first map and when the game…
  • querySelector and querySelectorAll vs…
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • How to create a drop shadow only on one side of an element?
  • JS: iterating over result of getElementsByClassName…
  • Using two values for one switch case statement
  • Calculator: Back key doesnt work in Javascript
  • Uncaught Type Error: View is not a constructor
  • How to enable Keyboard navigation between…
  • Parsing Nested Polymorphic Objects with GSON and Retrofit
  • How to change the background color on a input…
  • One to many relationship in two JSON arrays, lookup…
  • Using setInterval for creating animation
  • Unable to get Notification pop-up
  • Why isnt the variable isValid updating properly?
  • Polymer 2 access file uploaded in another page
  • Form field border-radius is not working only on the…
  • Polymer 1.0: Sorting iron-list
  • Issue in the shell script sending email
  • How can I pass a wct test while rearranging children…
  • ConstraintLayout - center and constraint
  • Why is Ember throwing "Uncaught Error: Assertion…
  • Why doesnt my table sort my div variable in numerical order?
  • Javascript and css animation
  • Getting or changing CSS class property with…
  • Named tuple and default values for optional keyword…
  • jQuery .on('change') function not working after…
  • Why do boxes become checked and then unchecked after…
  • LNK2019 símbolo externo public: bool __thiscall ……
  • Can't find why the if block not getting executed
  • How to set width of mat-table column in angular?
  • JavaScript click event listener on class
  • I have these 2 tonnage calculators. The first one is…
  • Changing an image with a dropdown, only works with…
  • addEventListener on home page only
  • How to submit Polymer forms to PHP and display response
  • Reset/remove CSS styles for element only
  • Builder pattern without inner class
  • how to filter out a null value from spark dataframe
  • jquery get all input from specific form
  • Jquery If radio button is checked
  • Using IS NULL or IS NOT NULL on join conditions -…
  • Vue.js input element values are empty
  • Ember 2, filter relationship models (hasMany,…
  • jQuery Mobile: document ready vs. page events
  • Vue CLI 3 / Webpack production build not working on…
  • For-each over an array in JavaScript
  • Polymer 1.x: How to data bind to a variable boolean…
  • I made a game it should display two images when i…
  • Uncheck radio button
  • How to have multiple functions in onkeyup?
  • Grow left and right css animation
  • How to use a template without a shadowRoot?
  • Error stopping function after alert message is shown
  • detect dynamically checkboxes/polymer elements…
  • Get whole selected Column value in the HTML table
  • jQuery: Check if special characters exists in string
  • I am trying to subtract but it is always adding one…
  • showing record text with red color when click stop button
  • Javascript Click on Element by Class
  • Trouble with boxes appearing/hiding based on selection
  • In CSS Flexbox, why are there no "justify-items" and…
  • iCheck doesn't work with VueJS
  • binary search tree is not working properly, but the…
  • Error trying module loading ( AMD ) with require.js
  • jquery to loop through table rows and cells, where…
  • What methods of ‘clearfix’ can I use?

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Finding the number of days between two dates

Next Post:

Converting double to string

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error