Uncaught SyntaxError: Unexpected identifier with radio buttons

I have a list of radio buttons. When one is clicked, I want to call a function to change some text on the page. Webstorm does not show any errors in the code, and it looks fine to me, but when I click one of the radio buttons in a browser I get "Uncaught SyntaxError: Unexpected identifier", specifically pointing to the last > in the code.

This is for a project and I desperately need to make this work! Any help would be greatly appreciated. I'm happy to add more of the code if needed. Thanks!

<input type="radio" onclick="showPrice(<%= rs[0] %>);" 
                       name="selectedService" value="<%= rs[0][i].service_id %>" 
                       id="selectedService<%= i %>">

EDIT: Per request, here is the contents of showPrice(object). #servicesForm is the id for the form with the radio buttons.

    function showPrice(services) {
        serviceID = $('input[name=selectedService]:checked', '#servicesForm').val();
        for ( i = 0; i < services.length; i++ ) {
            if ( services[i].service_id == serviceID ) {
                checkedValue = i;
        price = services[checkedValue].monthly_price;

Answers 1

  • I have found the solution! Many thanks to those who helped me :D. I needed to display monthly_price based on which radio button was selected, so I set each button's id attribute equal to the monthly_price for that specific element in rs[0] in the for loop. I then got the value of that attribute and set the text I wanted to change equal to that value in showPrice(). Below is the code.

        function showPrice() {
            $("#servicePriceString").text($('input[name=selectedService]:checked', '#servicesForm').attr('id'));

    . . .

    <% for ( var i=0; i < rs[0].length; i++) { %>
                <label for="<%=rs[0][i].monthly_price%>">
                <div id="serviceAvailableArea">
                    <input type="radio" id="<%=rs[0][i].monthly_price%>" onclick="showPrice()"
                           name="selectedService" value="<%= rs[0][i].service_id %>">
                    <%= rs[0][i].service_name %> at <%= rs[0][i].speed %>Mbps
            <% }%>

Related Articles