Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I have 3 buttons with same id ...</p> </blockquote> <p>You have invalid HTML, you can't have more than one element in a page with the same <code>id</code>.</p> <p><a href="http://www.w3.org/TR/html401/struct/global.html#h-7.5.2">Quoting the spec</a>: </p> <blockquote> <p><em>7.5.2 <strong>Element identifiers: the id and class attributes</em></strong> </p> <p><strong>id = name [CS]</strong><br> This attribute assigns a name to an element. This name must be unique in a document.</p> </blockquote> <p><strong>Solution</strong>: change from <code>id</code> to <code>class</code>,</p> <pre><code>&lt;button type="button" class="btn btn-primary xyz" value="1"&gt;XYZ1&lt;/button&gt; &lt;button type="button" class="btn btn-primary xyz" value="2"&gt;XYZ2&lt;/button&gt; &lt;button type="button" class="btn btn-primary xyz" value="3"&gt;XYZ3&lt;/button&gt; </code></pre> <p>And the <strong>jQuery code</strong>:</p> <pre><code>$(".xyz").click(function(){ alert(this.value); // No need for jQuery :$(this).val() to get the value of the input. }); </code></pre> <blockquote> <p>But it works only for the first button</p> </blockquote> <p>jQuery <code>#id</code> selector <a href="http://api.jquery.com/id-selector/">docs</a>:</p> <blockquote> <p>Each id value must be used only once within a document. <strong>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.</strong> This behavior should not be relied on, however; a document with more than one element using the same ID is invalid.</p> </blockquote> <p>If you look at the jQuery source you can see when you call <code>$</code> with an id selecor-(<code>$("#id")</code>), jQuery calls the native javascript <code>document.getElementById</code> function:</p> <pre><code>// HANDLE: $("#id") } else { elem = document.getElementById( match[2] ); } </code></pre> <p>Though, in the <a href="http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-getElBId">spec</a> of <code>document.getElementById</code> they didn't mention it must return the first value, this is how most of (maybe all?) the browsers implemented it.</p> <p><strong><a href="http://jsfiddle.net/CAnxW/">DEMO</a></strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload