0

there is something weird happening on my webpage.

I want to add a confirmation jquery dialog when I click a button. The problem is....I have that button multiple times on my page, each one having its "id" and is created by php (so there can be 2,3,4,5 buttons on a page).

The button looks like this:

            <a href="#" id="confirm" onclick="addlist(<?php echo $v['id'];?>);return false;"><img src="img/item_add.png" alt="add" width="100" height="46" /></a>

And this is my jquery

  $(document).ready( function(){

$('#dialog').dialog({
    autoOpen: false,
    width: 600,
    buttons: {
        "Ok": function() {
            $(this).dialog("close");
        },
    }
});

$('#confirm').click(function() {
    $('#dialog').dialog('open');
    return false;
    });
});​

The problem is that, my jquery code is working just fine, but only for the first button, the second, third...etc not

you have here my example: http://jsfiddle.net/S4vSU/1/

I want to have that dialog working for every button. Where is the problem ?

Thank you in advance !

5 Answers 5

5

"id" of particular element must be unique on whole page....
if not then only one(first) element will be treated....

solution: give some class to all buttons and instead of id process by class name

e.g:

<a class="temp" href="#" id="confirm" onclick="addlist(<?php echo $v['id'];?>);return false;">
    <img src="img/item_add.png" alt="add" width="100" height="46" />
</a>

and other code will be

$(document).ready( function(){

$('#dialog').dialog({
    autoOpen: false,
    width: 600,
    buttons: {
        "Ok": function() {
            $(this).dialog("close");
        },
    }
});

$('.temp').click(function() {
    $('#dialog').dialog('open');
    return false;
    });
});​
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks ! Working as charm. Point for you, but to be fair, I will accept as answer the one who posted the solution first :)
1

Either use class or use different id's like:

$('#confirm1, #confirm2').click(function() {
    $('#dialog').dialog('open');
    return false;
    });
});

OR using class: jsFiddle

1 Comment

Thanks ! Working as charm. You was the first one with the class idea, so you're the winner. Thank you so much !
1

id should be unique for each item.. but you could try to get away using on() although it's not recommended:

http://jsfiddle.net/S4vSU/7/

$(document).on('click','#confirm', function() {
    $('#dialog').dialog('open');
    return false;
    });
});​

1 Comment

Working in your example, but not on my page :| Weird. However, changing the id for the class is much easier and better. Thank you anyway ;) Point for you.
1

this is because all your buttons have same id.Id of an element in html should be unique.

You can keep all having same class as I do here

1 Comment

Thanks ! As you said, I could use the class instead the id. Point for you, but to be fair, I will accept as answer the one who posted the solution first :)
1

You can use class instead of ID for the element and in click function too.

<a class="confirm" href="javascript:;">Confirm 1</a>
<a class="confirm" href="javascript:;">Confirm 2</a>
...
$('.confirm').click(function() {
    $('#dialog').dialog('open');
    return false;
    });
});​

1 Comment

Thanks ! That was the solution :) Point to you !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.