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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:08:29+00:00 2026-06-06T06:08:29+00:00

I have 3 buttons with same ID. I need to get each button’s value

  • 0

I have 3 buttons with same ID. I need to get each button’s value when it’s being clicked.

<button id="xyz" type="button" class="btn btn-primary" value="1">XYZ1</button>
<button id="xyz" type="button" class="btn btn-primary" value="2">XYZ2</button>
<button id="xyz" type="button" class="btn btn-primary" value="3">XYZ3</button>

Here is my current jQuery script:

$("#xyz").click(function(){
      var xyz = $(this).val();
      alert(xyz);
});

But it works only for the first button, clicking on the other buttons are being ignored.

  • 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-06T06:08:30+00:00Added an answer on June 6, 2026 at 6:08 am

    I have 3 buttons with same id …

    You have invalid HTML. You can’t have more than one element in a page with the same id attribute value.

    Quoting the spec:

    7.5.2 Element identifiers: the id and class attributes

    id = name [CS]
    This attribute assigns a name to an element. This name must be unique in a document.

    Solution: change from id to class:

    <button type="button" class="btn btn-primary xyz" value="1">XYZ1</button>
    <button type="button" class="btn btn-primary xyz" value="2">XYZ2</button>
    <button type="button" class="btn btn-primary xyz" value="3">XYZ3</button>
    

    And the jQuery code:

    $(".xyz").click(function(){
        alert(this.value);
        // No need for jQuery :$(this).val() to get the value of the input.
    });
    

    But it works only for the first button

    jQuery #id selector docs:

    Each id value must be used only once within a document. If more than one element has been assigned the same ID, queries that use that ID will only select the first matched element in the DOM. This behavior should not be relied on, however; a document with more than one element using the same ID is invalid.

    If you look at the jQuery source you can see when you call $ with an id selecor-($("#id")), jQuery calls the native javascript document.getElementById function:

    // HANDLE: $("#id")
    } else {
        elem = document.getElementById( match[2] );
    }
    

    Though, in the spec of document.getElementById they didn’t mention it must return the first value, this is how most of (maybe all?) the browsers implemented it.

    DEMO

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

Sidebar

Related Questions

I have two buttons with the same ID: <button type=submit onclick=//do something id=theID>button 1</button>
I have buttons on a page that look like: <p> <a href=view.php> <input type=button
I have divs with same class, but each 3 are wrapped in a parent
I have a UITableViewCell with 3 checkmark buttons. Each checkmark button is a custom
I have three radio buttons with same name and different values.When I click the
I have 3 google +1 buttons in the same wordpress page. But one of
I have WPF Form which has many buttons with the same code. Appearance of
I have a .xhtml with two commandlink buttons and two outputpanels. Everything inside same
I have a simple button (3 of them - all same except the content
In my application I have 2 separate ways to get to the same UIViewController,

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.