guys
this is my html part :
<div class="radio">
<input type="radio" name="certain" id="oui" value="oui"/>
<label for="oui">
oui
</label>
<br />
<input type="radio" name="certain" id="non" value="non" checked="checked"/>
<label for="non">
non
</label>
</div>
guys
this is my html part :
<div class="radio">
<input type="radio" name="certain" id="oui" value="oui"/>
<label for="oui">
oui
</label>
<br />
<input type="radio" name="certain" id="non" value="non" checked="checked"/>
<label for="non">
non
</label>
</div>
var espece = $('#espece');
var poil = $('#poil');
var _certain = $('[name = "certain"]');
var certain = null;
_certain.each(function(index, element){
if(_certain[index].checked){
certain = element;
}
});
var continent_valeur = $('select option:selected').val();
/* Test des champs */
if(espece.val() == '' || poil.val() == '' || certain.val() == '' || continent_valeur == ""){
alert("Veuillez remplir tous les champs !");
}
When i submit my form, i have an error on my var certain in the condition if. It says certain.val() is not a function.
But var certain contains the good value of my radio yes or not.
i don't understand can you help me please ?
Share Improve this question asked Apr 22, 2018 at 19:50 jayensenjayensen 472 silver badges8 bronze badges 5-
$('[name = "certain"]')
isnull
try removing the spaces$('[name="certain"]')
– Benjamin RD Commented Apr 22, 2018 at 19:53 -
but
certain = element
after,var certain = null
, now certain worth the object input no ? – jayensen Commented Apr 22, 2018 at 19:55 - Try 'certain.value' instead! – Johnsackson Commented Apr 22, 2018 at 19:56
-
$('[name = "certain"]')
is not null when I write$('[name = "certain"]').val();
– jayensen Commented Apr 22, 2018 at 19:58 - or certain = $(element); in js line no.8 – Johnsackson Commented Apr 22, 2018 at 20:00
4 Answers
Reset to default 4When you use each, element represents a non-jquery node. You just need to change
certain = element;
to
certain = $(element);
and you should be to go.
Any JQuery questions you may have can be found from W3Schools:
- https://www.w3schools./jquery/default.asp
and yours specifically:
- https://www.w3schools./jquery/misc_each.asp
val()
is a jQuery method that is only applicable to jQuery objects and not valid for plain JavaScript elements.
If you want to use plain vanilla JS then you have to use certain.value
or in case of jQuery you can use $(certain).val()
.
Refer code:
var espece = $('#espece');
var poil = $('#poil');
var _certain = $('[name = "certain"]');
var certain = null;
_certain.each(function(index, element) {
if (_certain[index].checked) {
certain = element;
}
});
var continent_valeur = $('select option:selected').val();
/* Test des champs */
if (espece.val() == '' || poil.val() == '' || $(certain).val() == '' || continent_valeur == "") {
alert("Veuillez remplir tous les champs !");
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="radio">
<input type="radio" name="certain" id="oui" value="oui" />
<label for="oui">
oui
</label>
<br />
<input type="radio" name="certain" id="non" value="non" checked="checked" />
<label for="non">
non
</label>
</div>
You're getting this because the val function is to be applied over a jquery object, try this instead
$(element).val();
Also, in your each there's no need of doing _certain[index].checked since you already get the element in the second parameter of the callback.
When you iterate through a jQuery collection of elements with .each
, it provides you with the index and the element itself. The second argument isn't a jQuery-wrapped element - it's the plain element.
Either use the ordinary Javascript method to get the value from the element:
`certain.value`
Or select it with jQuery first:
`$(certain).val()`