How to check which radio button is checked with jquery?

I'm trying to print text depending on which radio button is checked. Can you tell me what's wrong?

I'm trying to print text depending on which radio button is checked. Can you tell me what's wrong?

Just getting the "error" output.

<input type="radio" name="feedback-form-visit-again" id="genderm" class="gendercheck" value="0" required> m
<input type="radio" name="feedback-form-visit-again" id="genderw" class="gendercheck" value="1"> w

<span class="gendertest">Output</span>
$(document).ready(function() {
$(".gendercheck").change(function() {
if (gender.value == 0) {
$(".gendertest").text("m");
} else if (gender.value == 1) {
$(".gendertest").text("w");
} else {
$(".gendertest").text("error");
}
});
});


Using The jQuery .each() Function

<strong>jQuery each</strong>&nbsp;is one of the better-used functions in&nbsp;<strong>jQuery</strong>&nbsp;so I think it’s great to understand what you can do with it.

jQuery each is one of the better-used functions in jQuery so I think it’s great to understand what you can do with it.

The jQuery each function is used to loop over data the accessible way to think of it is that it’s similar to a foreach loop in other languages. So you can use it to loop over a number of DOM objects from the same selectors for a case if you want to add a target=”blank” to all links on the page then you will select all associations and loop through each of them to add a target=”blank”.

$('a').each(function(i){
    $(this).attr('target', '_blank');
});

Let us investigate how this works. Earliest in order we get all the anchor links on the page by using the following selector.

// Get all anchor links
$('a')

Next we adoption each function to loop through all the links.

$('a').each(function(i){
    // Performs tasks to each of the links
});

When you’re central the each function you can access the current element it is the loop through by using this keyword, but this article will not be a jQuery object and therefore if it’s a DOM aspect you will not be able to use any jury functions on it. The solution to this complication is to wrap this inside a jQuery object defined.

$('a').each(function(i){
    // Performs tasks to each of the links
    $(this);
});

When we have the current bashed element inside a jQuery object we can then add a new aspect to the link by using the attr function.

$('a').each(function(i){
    $(this).attr('target', '_blank');
});
GET THE CURRENT INDEX OF THE LOOP

In the above example, you’ll attend the I inside the function() parameters. This variable is colonized with the current index of the each to see this alive have 10 links on the page.

<a>Link 1</a>
<a>Link 2</a>
<a>Link 3</a>
<a>Link 4</a>
<a>Link 5</a>
<a>Link 6</a>
<a>Link 7</a>
<a>Link 8</a>
<a>Link 9</a>
<a>Link 10</a>

Then output the current index by watchful this to the user.

$('a').each(function(i){
    alert(i);
});
LOOP THROUGH ARRAYS

In the above example, you saw how you can select DOM elements and loop through them but you can also use it to curve through arrays of data and get both the indicator and the value of the area inside the array.

var fruit = ['orange', 'apple', 'banana', 'grapes', 'kiwi'];

$.each(fruit, function(index, value){
console.log(index + ' ' + value);
});

In the console, it will output the following.

“0 orange” “1 apple” “2 banana” “3 grapes” “4 kiwi”

LOOP THROUGH OBJECTS

What if you’re using objects to store data and not batch, each function will take care of that the same way as you can notice in the following code.

var fruitObj = {
1: 'orange',
2: 'apple',
3: 'banana',
4: 'grapes',
5: 'kiwi'
};

$.each(fruitObj, function(key, value){
console.log(key + ' ' + value);
});

The output of looping over the object is below.

“1 orange” “2 apple” “3 banana” “4 grapes” “5 kiwi”

See more:

jQuery Cheat Sheet – All in one

Javascript vs jQuery Examples


By : i'm programmer

jQuery Full Course - jQuery Tutorial From Beginner to Advanced - Learn jQuery 2019

jQuery is a cross-platform JavaScript library designed to simplify the client-side scripting of HTML. In this video, you'll learn jQuery from Beginner to Advanced

jQuery is open-source software using the permissive MIT License. Web analysis indicates that it is the most widely deployed JavaScript library by a large margin.

Mastering jQuery will boost up your career specially in web development. This course is designed you to master yourself in jQuery through step by step process.

Course Contents

  • jQuery Intro
  • What is jQuery and what you will learn?
  • Downloading jQuery
  • jQuery Basics
  • A first look at jQuery code
  • Selectors and Filters
  • Replacing contents
  • Handling events
  • Hide/Show events
  • Fading
  • Slide
  • Toggle
  • Animate
  • Selectors
  • Filters
  • jQuery Advanced
  • Advanced Selectors
  • Creating Content
  • Creating Content: Part 2
  • Inserting content
  • Modifying content
  • Modifying CSS
  • Final thoughts


What you’ll learn

  • Will become Master in client side web development
  • Will become expert in jQuery
  • Will earn working experience on jQuery
  • Are there any course requirements or prerequisites?
  • Prior experience on HTML and CSS required.
  • Who this course is for:
  • Who wanna expert in client side web development
  • Who wanna expert in jQuery


Thanks for watching

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Access variable from function inside of nested function

In PHP, you have to use the&nbsp;global&nbsp;keyword in order to access a variable from the global scope when inside of a function. However, how can you access a variable within the scope of a parent function?

In PHP, you have to use the global keyword in order to access a variable from the global scope when inside of a function. However, how can you access a variable within the scope of a parent function?

In the example:

function foo() {
    $content = 'foobar';
function bar() {
    echo $content; // echos nothing
}

}

How can I access the $content variable?