How I can get the value of the input type radio using jQuery the value contains spaces

get selected value of radiobuttonlist in jquery
jquery get radio button value on change
javascript radio button value if checked
jquery set radio button checked based on value
how to get multiple radio button value in jquery
how to get radio button value in jquery by id
how to get selected radio button value in javascript
javascript select radio button based on value

I used this code I also used AJAX in my page

$html.='<div><p id="question" name="qid">Q.'.$result['question_no'].': '.$result['question'].'</p></div>
     <label style="padding-top:15px;">
        A <span style="padding-left: 15px;"><input type="radio"';
        if (isset($result['answer']) && $result['answer']==$result['choice1']) {
            $html.= 'checked="checked"';
        } 
         $html.=' name="answer" id="option" value='.$result['choice1'].'>'.$result['choice1'].'</span>
     </label>
    </div>

And the jQuery part is

var answer = $("form input[type='radio']:checked").val();
alert(answer); 

The value contain spaces like 'primary key' but I got only primary how can I get value with spaces

The solution is really simple you just forgot to put double quotes for the value attribute, value attribute must be wrapped in double quotes here is the corrected code,

$html.='<div><p id="question" name="qid">Q.'.$result['question_no'].': '.$result['question'].'</p></div>
     <label style="padding-top:15px;">
        A <span style="padding-left: 15px;"><input type="radio"';
        if (isset($result['answer']) && $result['answer']==$result['choice1']) {
            $html.= 'checked="checked"';
        } 
         $html.=' name="answer" id="option" value="'.$result['choice1'].'">'.$result['choice1'].'</span>
     </label>
    </div>';

Now the jquery will pick the value correctly

jQuery get value of selected radio button, To get the value of the selected radio button, select it by name with the :checked filter. var selectedVal = ""; var No space should be used in between selector. value var RadeoButtonStatusCheck = $('form input[type=radio]:checked').val();. I have the code retrieving the checked radio value into the #donation text field however I want to set up an additional readonly that shows the selected value whether it was a radio or what was keyed up in the text field.

Why don't you ensure that the value comes in snake_case like primary_key, then process it to what you want after getting it with JQuery. Something like:

var answer = $("form input[type='radio']:checked").val();
alert(answer.split("_"));

.val(), Get the current value of the first element in the set of matched elements or set used to get the values of form elements such as input , select and textarea . set)​, .val() returns an array containing the value of each selected option. This example removes leading and trailing whitespace from the values of text inputs with a  This tutorial, Explains about, how to get input values of different HTML form elements using jQuery as follows: we have taken three form elements in our example – input field, radio button and a textarea. To get value of input field.

hi here is a working solution for your problem. feel free to modify the code to make it fit for your needs

 <!-- including jquery library -->
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        /* When input of type button is clicked then call this function */
        $("input[type='button']").click(function(){
            /* load value of input with name gender in a JS variable radioValue */
        	var radioValue = $("input[name='gender']:checked").val();
            /* check if there is some value in radioValue variable... if there is no value then if block will not execute */
            if(radioValue){
                alert(radioValue);
            }
        });
        
    });
</script>
 
        <input type="radio" name="gender" value="Gender male">Male
        <input type="radio" name="gender" value="Gender female">Female
        <input type="button" value="Get Value"></p>
                           

How to set the radio button selected value using jQuery in , How to set the radio button selected value using jQuery in RadioButtonList? input id = "rblSex_0" type = "radio" name = "rblSex" value = "Male" /> In the above generated HTML, all the radio buttons would have the same selector would not be able to select the elements who value contain spaces. In this jquery snippets, you will know how to retrieve selected or checked the radio button it’s value. You can use the selector Like(#id, .class, element, :header, el1,el2,el3) with val() method by jQuery CDN Api with retrieve the value of selected/checked or radio input or button value. Answer: Use the jQuery CDN :checked selector

How to get value of selected radio button using JavaScript , If there are multiple Radio buttons in a webpage, first, all the input tags are fetched and then the values of all the tags that have type as 'radio' and are selected  Answer: Use the jQuery :checked selector You can simply use the jQuery :checked selector in combination with the val() method to find the value of the selected radio button inside a group. Let's try out the following example to understand how it basically works:

How to get all selected checkboxes in an array using jQuery , portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and If there are multiple event values, separate them by space. input type = "checkbox" name = "type" value = "Geeks" /> Geeks: How to get value of selected radio button using JavaScript? For you people living on the edge: There is now something called a RadioNodeList and accessing it's value property will return the value of the currently checked input. This will remove the necessity of first filtering out the 'checked' input as we see in many of the posted answers.

Introduction to JavaScript selectors, Using JavaScript selectors on the values of elements, you can add, modify, or delete Laserfiche Forms uses the popular JavaScript library jQuery. In $('​input[type="text"]') selects all input elements that have the type text . To select descendants of an element: Separate the ancestor and the descendant with a space. Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

Comments
  • While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.