2

I try to get this code to work with the 3 input fields, but it works only with the first :

<form>  
<p><input id="qty" type="text" maxlength="1"  /></p>
<p><input id="qty" name="text" type="text"  /></p>
<p><input id="qty" name="text2" type="text" /></p>
</form>

<script type="text/javascript">

$("#qty").change(function(e) {
    if(this.value != '3' && this.value != '6' && this.value != '9') {
        this.value = 0;         
        alert('You can buy only 3, 6, or 9 pieces fromn this product');
    } });

</script>

What can be my error ?

Town
  • 14,706
  • 3
  • 48
  • 72
Bizboss
  • 7,792
  • 27
  • 109
  • 174

8 Answers8

2

You cannot have 3 things with the same ID try using classes instead:

<form>  
<p><input class="qty" type="text" maxlength="1"  /></p>
<p><input class="qty" name="text" type="text"  /></p>
<p><input class="qty" name="text2" type="text" /></p>
</form>

<script type="text/javascript">

$(".qty").change(function(e) {
    if(this.value != '3' && this.value != '6' && this.value != '9') {
        this.value = 0;         
        alert('You can buy only 3, 6, or 9 pieces fromn this product');
    } });

</script>
Naftali
  • 144,921
  • 39
  • 244
  • 303
1

You cannot have multiple elements with the same ID.

Use a classname instead.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

It only works on the first, because you're connecting the event to the ID, and using the same ID multiple times. IDs can only be used once on a given page in order to be useful.

Referencing element IDs in jQuery will only match the first occurrence in the DOM - this is how they work. Use a class instead.

jefflunt
  • 33,527
  • 7
  • 88
  • 126
1

Your problem is your use of the id attribute.

An id is unique. Therefore only ONE element on the page can have the id qty.

Use class instead and then all three can have class='qty'.

And change this to $('this')

Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224
  • remove that last part, and this is a fully correct answer and deserves a `+1` – Naftali Jun 28 '11 at 15:51
  • No. See http://stackoverflow.com/questions/1051782/jquery-this-vs-this - `this == $('this')[0]` be careful of this. When dealing with jQuery ALWAYS use `$('this')` – Thomas Clayson Jun 29 '11 at 08:40
1

Try giving all the text boxes unique ids.

Jasper
  • 75,717
  • 14
  • 151
  • 146
0

You cannot have more than one element with the same id. Use a class instead.

Also embed you change event inside document ready event.

$(function(){
    $("input:text.myclass").change(function(e){
        if(this.value != '3' && this.value != '6' && this.value != '9') {
            this.value = 0;         
            alert('You can buy only 3, 6, or 9 pieces fromn this product');
        }
    });
});
rahul
  • 184,426
  • 49
  • 232
  • 263
0

ID attribute is a unique identifier of an element and you can't have duplicates within a page. So change your IDs to class names and you'll be good to go:

<p><input class="qty" type="text" maxlength="1"  /></p>
<p><input class="qty" name="text" type="text"  /></p>
<p><input class="qty" name="text2" type="text" /></p>

Also, you have to reference the element via jQuery wrapper when you're dealing with it within jQuery context. this becomes $(this) and value property becomes val() function call.

Change to this:

$(".qty").change(function(e) {
  var value = $(this).val();

    if(value != '3' && value != '6' && value != '9') {
        $(this).val(0);         
        alert('You can buy only 3, 6, or 9 pieces fromn this product');
    }
});
Kon
  • 27,113
  • 11
  • 60
  • 86
-1

I'm not sure if this is the solution, but in jQuery you have to use $(this) instead of this. Then, you also have to use .val() instead of value.

So you have to use something like this:

$(this).val()

To get the value of a field, to set the value, use this:

$(this).val('new value')

Edit: After you added the code: Like, others said, you can't use an id multiple times.

Edit 2: Ignore my answer, totally wrong. Excuse me.

Ray
  • 1,192
  • 6
  • 10