Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stuck using onClick opening Dialog box

Tags:

jquery

Very new to this and am stuck already with an onClick command and it's puzzling me.

What's meant to happen: User clicks on the Shopping Cart text (within a div element which has been styled) and it opens a dialog box containing the users cart contents.

Here's the code below...

I think I've done a tad to much this week as I'm probably missing something really easy and am just being stupid.

Any help would be much appreciated.

Copy code

<script type="text/javascript">
$(function() {
$( 'div.dialog' ).dialog( {modal:true,autoOpen:false} );
$('CartLink').on( 'click', function() {
    var index = $(this).index() + 1;
    $( '#dialog' +  index ).dialog( 'open' );
});
});
</script>

    <div class="dialog" id="dialog1">Shopping Cart Contents</div>

<div id="CartLink" class="fluid ShoppingCart"><img src="images/Site/Shopping_cart.gif"         
alt="" width="25" height="23"/>Shopping Cart</div>
like image 346
Earunder Avatar asked Jan 28 '26 11:01

Earunder


2 Answers

You are missing # before the id selector

$(function() {
    $( 'div.dialog' ).dialog( {modal:true,autoOpen:false} );
    $('#CartLink').on( 'click', function() {
    //.^.....add # here.......
        var index = $(this).index();
        $( '#dialog' +  index ).dialog( 'open' );
    });
});
like image 178
Pranav C Balan Avatar answered Feb 03 '26 01:02

Pranav C Balan


Seem like you're missing # to target id here

$('#CartLink').on( 'click', function() {
//-^ here -----
    var index = $(this).index() + 1;
    $( '#dialog' +  index ).dialog( 'open' );
});

You need to change:

var index = $(this).index() + 1;

to:

var index = $(this).index();

because currently the index of your #CartLink is 1 not 0 as what you're expected. So you don't need to increase it by 1 anymore.

like image 35
Felix Avatar answered Feb 02 '26 23:02

Felix



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!