Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7651383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:29:32+00:00 2026-05-31T11:29:32+00:00

I am having a problem when trying to use each() twice. I have a

  • 0

I am having a problem when trying to use each() twice.

I have a list of radio checked buttons of which each has a datasrc of a website.

Example:

<input type="radio" checked datasrc="www.john.com" id="John">John<br/>
<input type="radio" checked datasrc="www.maria.com" id="Maria">Maria<br/>
<input type="radio" datasrc="www.joe.com" id="Joe">Joe<br/>​

I want to retrieve each checked radio button so I do this:

$("input:radio").each(function(){

var name = $(this).attr("id");


    if($("[id="+name+"]:checked").length == 1)
    {
        var src = $('#' + name).attr("datasrc")                                                                                                                                                                                                                                                                                                                                                                                                                                                                     

      console.log(name);
      console.log(src);                        

    }                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
});

Now when I retrieve every checked radio button, I want to append it with id its id and for value, its datasrc. For example:

<div id="John">www.john.com</div>
<div id="Maria">www.maria.com</div>

When I tried using each again I get manage to get it printed but several times. For example john will print 4 times and maria will print 5 times (the amount of the id).

For example:

$("input:radio").each(function () {

   var name = $(this).attr("id");

   if ($("[id=" + name + "]:checked").length == 1) {
      var src = $('#' + name).attr("datasrc")

      var html = "";
      for (i = 0; i < name.length; i++) {
         html += "<div id='" + name + "'>" + src + "</div>"
      }

      $("body").append(html);


   }

});

Will print:

www.john.com
www.john.com
www.john.com
www.john.com
www.maria.com
www.maria.com
www.maria.com
www.maria.com
www.maria.com

What I’m I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:29:33+00:00Added an answer on May 31, 2026 at 11:29 am

    It’s because you’re nesting a for loop inside each so the results of the for loop run as many times as the each loop…You don’t need the for loop though, a simple array and and each() will work:

    Edit: Made it a function so you can use it at any time.

    var getUrls = function () {
    
        var urls = [];
    
        $('input:radio').each(function () {
    
            var $this = $(this),
                id = $this.attr('id'),
                url = $this.attr('datasrc');
    
            if ($(this).prop('checked')) {
                urls.push('<div class="' + id + '">' + url + '</div>');
            }
    
        });
    
        return urls;
    
    };
    
    $('body').append(getUrls().join(''));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a problem trying to use the prependTo() function in jQuery... for
I'm having problems trying to use the friend feature of C++. I have these
I'm having a problem with accessing my buttons separately using jquery. Suppose I have
I'm having trouble trying to understand how to use recursion with this problem. I'm
I'm trying to use HTTPWebRequest to access a REST service, and am having problems
I'm trying to use the SWI-Prolog JPL library, but I'm having problems. I'm trying
I'm having a problem trying to format the output on the jQuery UI datepicker.
I'm having a problem trying to create a Javascript function that checks all the
I'm having a problem trying to make eclipse and aspectj work for Dynamic Web
I'm having a problem when trying to pass an array back to a COM

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.