$(function() { var ids = new Array(); for(var i = 0; i <...">

Getting switch text instead of values

this is js

    <SCRIPT type="text/javascript">
    $(function() 
    {   

        var ids = new Array();

        for(var i = 0; i < <?php  echo $numR; ?>; i++)
        {
            ids.push(i);
        }

        function f(place) {
           $("#prod_btn" + place).click(function () {

                var combine = "prod_title" + place;
                var userNameVal = $("#" + combine).val();
                var radVal = $('input:radio[name=prod_rad' + place +']:checked').val();
                $.post( 
                 "cart.php",
                 { uval : userNameVal,
                    rval : radVal },
                 function(data) {
                    $('#show_search2').html(data);
                 }

              );


           });
        }

        for (var i=0; i<ids.length; i++) 
        {

           f(ids[i]);
        }

    });
</SCRIPT>

this is HTML

while ($i < $numR)
{
$prod_image_id = 'prod_image'.$i.'';
$prod_title_id = 'prod_title'.$i.'';
$prod_priceM_id = 'prod_priceM'.$i.'';
$prod_priceA_id = 'prod_priceA'.$i.'';
$prod_priceH_id = 'prod_priceH'.$i.'';
$prod_priceP_id = 'prod_priceP'.$i.'';
$prod_rad_id = 'prod_rad'.$i.'';
$prod_btn_id = 'prod_btn'.$i.'';

$prodID         = mysql_result($result, $i, "astm_product_ID");
$prodName       = mysql_result($result, $i, "astm_product_name");
$prodPic        = mysql_result($result, $i, "astm_product_image");

$mPrice         = mysql_result($result, $i, "astm_mill_finish_price");
$aPrice         = mysql_result($result, $i, "astm_anodised_price");
$hPrice         = mysql_result($result, $i, "astm_hanolok_price");
$pPrice         = mysql_result($result, $i, "astm_powdercoat_price");

    echo "<li>";
    echo "<DIV id='prod_container'>";
    echo "<DIV id = 'fp1'><center><IMG class = 'imagesize' src='images/fp1.png' id = '$prod_image_id'/></center></DIV>";
    echo "<DIV><p>$prodName<p><input type = 'hidden' id = '$prod_title_id' value = '$prodName' /></DIV>";
    echo "<DIV><input type='radio' value='$mPrice' name='$prod_rad_id'>Mill Finish - Php $mPrice</input></DIV>";
    echo "<DIV><input type='radio' value='$aPrice' name='$prod_rad_id'>Anodised - Php $aPrice</input></DIV>";
    echo "<DIV><input type='radio' value='$hPrice' name='$prod_rad_id'>Hanolok - Php $hPrice</DIV>";
    echo "<DIV><input type='radio' value='$pPrice' name='$prod_rad_id'>Powder Coat - Php $pPrice</input></DIV>";
    echo "<DIV><button type='button' id = '$prod_btn_id' onClick = 'fg_popup_form(\"fg_formContainer\",\"fg_form_InnerContainer\",\"fg_backgroundpopup\");'>Add to Cart</button></DIV>";
    echo "</DIV>";
    echo "</li>";

$i++;
}

js just gets the value of the selected radio button and product name. It is working fine. My problem is that I also want to get the text of the selected radio button. ".text ()" doesn't work for me, and I don't know why. Help me thanks! :)

+5
source share
2 answers

Using the form to access the selected radio button

tl; dr here is a live demo: http://jsfiddle.net/g105b/wchyLcfc/

A good trick is how browsers expose the selected form radio button by its name.

Consider this HTML with gender selection:

<form id="myForm">
    <label>
        <input type="radio" name="gender" value="m" />
        <span>Male</span>
    </label>

    <label>
        <input type="radio" name="gender" value="f" />
        <span>Female</span>
    </label>

    <label>
        <input type="radio" name="gender" value="o" />
        <span>Other</span>
    </label>
</form>

myForm, JavaScript :

var myForm = document.getElementById("myForm");
var selectedRadio = myForm["gender"];

:

selectedRadio.value // m, f or o
selectedRadio.parentElement.textContent // Male, Female or Other
+13

Data- * , , jquery, :

HTML
    <input id="radio4" name="radios1" type="radio" value="4" data-value="radio 4" /> radio 4
    <input id="radio5" name="radios1" type="radio" value="4" data-value="radio 5"/> radio 5

Script:
        $("input[name=radios1]:checked").attr("data-value");

Custom Data- * Attributes: http://www.w3schools.com/tags/att_global_data.asp

+1

All Articles