Search code examples
jqueryformsradio-button

cant set radio element in html from jquery


i need to set radio element in user rating 5 star component.

get source from here (https://codepen.io/jamesbarnett/pen/vlpkh)

i calculate user_rate from database data and need set it to radio element

second code is part of my .php code that dose not work;

first code is same code that only replace value with variable;

what is difference between two code?

first work and second dose not work :(

first code:(worked)

        <script>
        var user_rate=3;  //no need to this
        var $radios = $('input:radio[name=ratings]');
        $radios.filter('[value=3]').prop('checked', true);
        </script>

second code(dose not work)

        <script>
        var user_rate=3;  // value comes from database
        var $radios = $('input:radio[name=ratings]');
        $radios.filter('[value=user_rate]').prop('checked', true);
        </script>

Solution

  • Try doing this. You need to evaluate te variable and not pass it in the string directly.

    $radios.filter('[value=' + user_r + ']').prop('checked', true);