Radio group with function shows the value="text" when selecting, with checked="yes" by default it would have to show it also

0

I have a radio group, with a function that shows the value when I check the selection. It works well. I added a default option with checked="yes". that also applied well. By logic I assumed that the text of the option selected by default should also appear. After all, it is selected in the same way. I want that to happen. I leave a file where the example is

link

The html:

    <span class="ef-example-1-rating" id="odontologia">
    <input type="radio" class="ef-example-1-rating-input" id="ef-example-1-rating-input-1-5" name="ef-example-1-rating-input-1" value="Hello5" >
    <label for="ef-example-1-rating-input-1-5" class="ef-example-1-rating-star"></label>
    <input type="radio" class="ef-example-1-rating-input" id="ef-example-1-rating-input-1-4" name="ef-example-1-rating-input-1" value="Hello4">
    <label for="ef-example-1-rating-input-1-4" class="ef-example-1-rating-star"></label>
    <input type="radio" class="ef-example-1-rating-input" id="ef-example-1-rating-input-1-3" name="ef-example-1-rating-input-1" value="Hello3">
    <label for="ef-example-1-rating-input-1-3" class="ef-example-1-rating-star"></label>
    <input type="radio" class="ef-example-1-rating-input" id="ef-example-1-rating-input-1-2" name="ef-example-1-rating-input-1" value="Hello2">
    <label for="ef-example-1-rating-input-1-2" class="ef-example-1-rating-star"></label>
    <input type="radio" class="ef-example-1-rating-input" id="ef-example-1-rating-input-1-1" name="ef-example-1-rating-input-1" value="Hello1" checked="yes">
    <label for="ef-example-1-rating-input-1-1" class="ef-example-1-rating-star"></label>
    <br />
    <p id="forro" style="color:red;"></p>
    </span>

This is the function:

    $(function() {
        var $radBtn = $("#odontologia");
        $radBtn.click(function() {
            var $radChecked = $radBtn.find(':radio:checked');
            $("#forro").text('')
                .append($radChecked.val());
        });
    });

Thanks for your cooperation.

    
asked by Hernan527 12.08.2018 в 11:11
source

1 answer

0

I see everything well, what I think is missing is that your code is executed when the page is loaded so that it shows the text of the cheked by default. It would be enough to add your code within this function:

$( document ).ready(function() {
ejemplo();
});

For that you must name your function to simply call it from here.

$(function() { var $radBtn = $("#odontologia");
$radBtn.click(function ejemplo(){ 
var $radChecked = $radBtn.find(':radio:checked'); 
$("#forro").text(''") .append($radChecked.val()); 
});
})
    
answered by 12.08.2018 в 14:15