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 9223871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:10:41+00:00 2026-06-18T04:10:41+00:00

Given the following html: <dt><span class=q-num>1</span>. What is one plus one?</dt> <dd><input type=radio value=a

  • 0

Given the following html:

<dt><span class="q-num">1</span>. What is one plus one?</dt>

<dd><input type="radio" value="a" name="1" required /> 4</dd>
<dd><input type="radio" value="b" name="1" required /> 7</dd>
<dd><input type="radio" value="c" name="1" required /> 2</dd>
<dd><input type="radio" value="d" name="1" required /> 8</dd>

<dt><span class="q-num">2</span>. What is two plus two?</dt>

<dd><input type="radio" value="a" name="2" required /> 4</dd>
<dd><input type="radio" value="b" name="2" required /> 7</dd>
<dd><input type="radio" value="c" name="2" required /> 2</dd>
<dd><input type="radio" value="d" name="2" required /> 8</dd>

<dt><span class="q-num">3</span>. What is three plus three?</dt>

<dd><input type="radio" value="a" name="3" required /> 4</dd>
<dd><input type="radio" value="b" name="3" required /> 7</dd>
<dd><input type="radio" value="c" name="3" required /> 2</dd>
<dd><input type="radio" value="d" name="3" required /> 6</dd>

I want to alert() the value of the nearest previous span[class='q-num'] when any input[type='radio'] is clicked. I’ve tried closest() but all I get is a blank alert..

  • 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-06-18T04:10:42+00:00Added an answer on June 18, 2026 at 4:10 am

    prevUntil is your friend:

    $(this).parent().prevUntil('dt').last().prev().find('span[class="q-num"]').text();
    

    Here’s the fiddle: http://jsfiddle.net/UPRBT/


    If your posted markup is your real code, you can just use the name property of the clicked radio button:

    $('input[type="radio"]').click(function () {
        alert(this.name);
    });
    

    Here’s the fiddle: http://jsfiddle.net/b4Sj8/


    A more efficient way would be to use this custom traversal method:

    $.each({
        'prevFind' : 'previousSibling',
        'nextFind' : 'nextSibling'
    }, function(method, dir) {
    
        $.fn[method] = function (selector) {
    
            var elems = [];
    
            this.each(function(i, item) {
                while (item = item[dir]) {
                    if (item.nodeType == 1) {
                        if ( $.find.matches(selector, [item]).length ) {
                            elems.push(item);
                            break;
                        }
                    }
                }
            });
    
            return this.pushStack(elems, method, selector);
        };
    });
    

    which you can then use like this:

    $(this.parentNode).prevFind('dt').find('.q-num').text()
    

    Here’s the fiddle: http://jsfiddle.net/b4Sj8/1/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following HTML fragment: <span name=foo class=foo-class> <input name=foo value=0 id=foo2_0 type=radio> <label
I have the following html code: <div class=Radios> <span class=radio> <input type=radio value=1> yes
Given the following HTML form: <form id=myform> Company: <input type=text name=Company value=ACME, INC./> First
I have a HTML snippet like the following <tr class=new> <td> <input name=id type=checkbox/>
The following gives me the error Uncaught SyntaxError: Unexpected identifier : $('span.xtro').html(''); $('span.xtro').html('<input type='button'
Given the following HTML and function: <input type=text onfocus=TextBoxFocus() id=txtName /> . function TextBoxFocus()
Given following html code: <ul> <li> <a href=somelink> <span class='someIconCss'></span> <span>home</span> </a> </li> [possibly
Given the following HTML on a page: <div class='test'> <p><span id='id1' class='test1 test2 test3'>test
given the following div element <div class=info> <a href=/s/xyz.html class=title>title</a> <span class=a>123</span> <span class=b>456</span>
Given the following html: <ul id=search-results> <li> <h3>Result 1</h3> <span class=some-info>Tag line</span> </li> <li>

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.