-3

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>
Jonas
  • 121,568
  • 97
  • 310
  • 388
  • You'll need to 1) Show us the DOM that you're running this against, 2) Tell us what you're trying to do, and 3) Tell us what "does not work" means. – T.J. Crowder Dec 08 '16 at 17:03
  • Also note that there's no need for `filter`. Your first example is the same as `$('input:radio[name=ratings][value=3]').prop('checked', true);` – T.J. Crowder Dec 08 '16 at 17:05
  • 2
    Possible duplicate of [How to use javascript variables in jquery selectors](http://stackoverflow.com/questions/5891840/how-to-use-javascript-variables-in-jquery-selectors) – JJJ Dec 08 '16 at 17:06

1 Answers1

1

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);
Michelangelo
  • 5,888
  • 5
  • 31
  • 50