How to return type radio value

Asked

Viewed 330 times

1

Good morning !

I’m having a little problem I’m trying to return the value of a radio field printing on the screen to give a return type to the user before making the upload click. I’m currently testing on a script I found, but without much success is returning me Nan, I tried to change from parseint to parseFlot but still unsuccessful.

<form>
    <p>Now, with CSS3: </p>
    <div class="cc-selector">
        <input id="visa" type="radio" name="credit-card" value="20" onfocus="calcularPrimeiro()" />
        <label class="drinkcard-cc visa" for="visa"></label>
        <input id="mastercard" type="radio" name="credit-card" value="30" onblur="calcularPrimeiro()" />
        <label class="drinkcard-cc mastercard"for="mastercard"></label>
    </div>
</form>

<div id="resultado"></div>

<script type="text/javascript">
function calcularPrimeiro() {
  var n1 = parseFloat(document.getElementsByClassName('credit-card').value, 0);
  document.getElementById('resultado').innerHTML = n1;
}
</script>
  • getElementsByClassName returns an array of elements. Probably the .value is returning Undefined.

  • Probably yes, I tried this way now to enter the array and display the value, but still unsuccessfully informs me that the value is without anything declared, Function calculatedFirst() { var n1 = parseFloat(Document.getElementsByClassName('credit-card')); var value = n1[0]. value; Document.getElementById('result'). innerHTML = value; }

3 answers

3


Instead of using a for complex to do this, just use querySelector to get the item you want, like this:

document.querySelector('input[name="credit-card"]:checked').value;

If it’s jQuery, like this:

$('input[name="credit-card"]:checked').val();

Exchange the onblur and onfocus for onchange, because if not the event will run before changing the value

function calcularPrimeiro() {
  var input = document.querySelector('input[name="credit-card"]:checked');
  var n1 = parseFloat(input.value, 0);
  document.getElementById('resultado').innerHTML = n1;
}
<div id="resultado"></div>

<form>
    <p>Now, with CSS3: </p>
    <div class="cc-selector">
        <input id="visa" type="radio" name="credit-card" value="20" onchange="calcularPrimeiro()" />
        <label class="drinkcard-cc visa" for="visa"></label>
        <input id="mastercard" type="radio" name="credit-card" value="30" onchange="calcularPrimeiro()" />
        <label class="drinkcard-cc mastercard"for="mastercard"></label>
    </div>
</form>

  • Thank you so much for the answer, the first answer using loop has already solved my problem.

  • @Does Ronaldom prefer to loop with many lines instead of just writing one with querySelector? I think I could review this more calmly.

  • Really dude, I tested again and it gets a lot less complicated to implement and it worked for what I needed, in case someone needs to edit my code as it turned out

1

If I understand correctly, you want to bring a preselected item, so was added the checked in the first item and load page is already made the call of the method that returns the selected value. I also changed the function to take the radios by name and check which one is selected. As for the event that fires, I changed to the onclick because thus, when the user selects the item, the function will be fired.

(function() {
      calcularPrimeiro()
})();

function calcularPrimeiro() {

	var radios = document.getElementsByName('credit-card');
	for (var i = 0, length = radios.length; i < length; i++)
	{
		if (radios[i].checked)
		{
			var n1 = radios[i].value;
      var itemId = radios[i].id;
			document.getElementById('resultado').innerHTML = itemId + " - " + n1;
			break;
		}
	}  
}
<form>
    <p>Now, with CSS3: </p>
    <div class="cc-selector">
        <input id="visa" type="radio" checked name="credit-card" value="20" onclick="calcularPrimeiro()" />
        <label class="drinkcard-cc visa" for="visa">20</label>
        <input id="mastercard" type="radio" name="credit-card" value="30" onclick="calcularPrimeiro()" />
        <label class="drinkcard-cc mastercard"for="mastercard">30</label>
    </div>
</form>

</br>
</br>

<div>Resultado: <div id="resultado"></div></div>
<div id="resultado"></div>

  • Perfect guy, just one more question for me to avoid gambiarras, is displaying the correct value, but if I want there to be another variable in the middle of this loop to know the name of what was selected would be possible? Or do you think it’s best to just buy the amount received to figure it out? I believe that is not the best to do because the value can repeat, resulting in an error

  • @Ronaldom I made an issue in the answer by taking the item id, it should be unique, then it should solve what you want

  • Thanks I will test here, while this I did an edition adding in if more I believe it is wrong, follows what I tried to do: if (radios[i].checked) { var n1 = radios[i]. value; if(n1==='primeiraOpcao'){ Document.getElementById('result'). innerHTML = n1; } if(n1=='secondaOpcao'){ Document.getElementById('result'). innerHTML = n1; } break; }

  • It worked, thank you very much !

  • I found a small problem and still could not solve, I believe that it is because the variables declared within is not global correct? I have a few more fields and I can’t make a simple sum with other values of other functions

  • I gathered everything within the same function to be able to catch the value of the varial, however it seems that it is returning in string and now I’m trying to turn into float

  • var total = int(n1 + N2); Document.getElementById('Total'). innerHTML = total;

  • In this case, it would be better to open another question on the site with all the code there, then it is easier for the people to be able to help. And it gets more organized too.

  • I’ve already accomplished what I needed !

Show 4 more comments

-1

I performed a test and a half that worked the way I want, but I believe it is not the right way and whether it will work if I have more fields or I would have to fill if.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form>
    <p>Now, with CSS3: </p>
    <div class="cc-selector">
        <input id="visa" type="radio" name="credit-card" value="10" onfocus="calcularPrimeiro()" />
        <label class="drinkcard-cc visa" for="visa" ></label>
        <input id="mastercard" type="radio" name="credit-card" value="20" onblur="calcularPrimeiro()" />
        <label class="drinkcard-cc mastercard"for="mastercard"></label>
    </div>
</form>

<div id="resultado"></div>

<script type="text/javascript">
function calcularPrimeiro() {
  var n1 = document.getElementById("visa").value;
  var n2 = document.getElementById("mastercard").value;
  if($(visa).prop("checked")){
  document.getElementById("resultado").innerHTML = n1;
}else{
    document.getElementById("resultado").innerHTML = n2;
}
}
</script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.