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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:35:01+00:00 2026-06-17T00:35:01+00:00

I have an li element that looks like this: <ol id=selectbox> <li class=boxes name=bears>bees</li>

  • 0

I have an li element that looks like this:

<ol id="selectbox">
    <li class="boxes" name="bears">bees</li>
    <li class="boxes" name="cakes">candles</li>
    <li class="boxes" name="wine">beer</li>
    <li class="boxes" name="spoon">fork</li>
    <li class="boxes" name="bench">chair</li>
    <li class="boxes" name="matches">fire</li>
    <li class="boxes" name="kindling">wood</li>
</ol>

When I click it, I would like to get the name ‘bears’, OR the text ‘bees’ into a variable (It would be helpful to know how to do both). How can I do this with jQuery selectors?
I’ve tried many things such as

var $radios = $('input[name="mybuttons"]');
$('#selectbox li').click(function(){
  var $btn = $(this).addClass('active');
  var idx = $btn.index();    
  var name = $('li['+idx+']').name();
});

and

var name = $('#li.active').name()

and

var name = $radios.eq(idx).prop('name')

etc.

  • 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-17T00:35:02+00:00Added an answer on June 17, 2026 at 12:35 am

    You can use $(this) in the click event context as it corresponds to the element is clicked.

    var name = $(this).attr('name');  // bears 
    
    var val = $(this).text();       // bees
    

    Code

    $('#selectbox li').click(function(){
      var $this = $(this);
      var $btn = $this.addClass('active');
      var name = $this.attr('name');
      var txt = $this.text();
      console.log("Name Attribute - " + name + " :: Text - "+ txt); 
    });
    

    Working Fiddle

    Also it is better to use data attributes for custom attributes so that the Document will be validated and compliant with the standard practices.

    So

    <li class="boxes" name="bears">bees</li>
    

    Should look like

    <li class="boxes" data-name="bears">bees</li>
    

    to acces it you can use either .attr() or the .data() methods

    var name = $this.attr('data-name');
    var name = $this.data('name');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an anchor element that looks like this <a href=url><img src=imgurl/>Text</a> I would
I have some data in an XML element that looks like this: <?xml version=1.0
I have a list of lists that looks like this: x[[state]][[year]] . Each element
Lets say that I have a HTML that looks like this: <div class=aaa><span>1</span></div> <div
I have some HTML that looks like this: <h2>Heading</h2> <div class='myClass'> <h2>Another Heading</h2> </div>
I have code that looks like this: <div class=tag>Order # :</div> <div class=data> <input
Let's say I have CSS that looks like this: #element {background-image: url(image1.png);} #element {background-image:
I have several data that looks like this: Vector1_elements = T,C,A Vector2_elements = C,G,A
I have a document that looks something like <root> <element> <subelement1 /> <subelement2 />
I have a function that looks somewhat like this: function(domObj) { var currentObj =

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.