0
function bet() {
    $('table img').css('cursor', 'pointer');
    $('table img').click(function () {
        yourBetNumber = $(this).slice(0, -1);       
        $('#item' + yourBetNumber).clone().appendTo('#yourbet');
        $('table').hide();
        $('table img').css('cursor', 'pointer');
    });
}

I have a table of items displayed, each item is a different image and has a unique ID in the html. What I'm trying to do is enable the user to click on the item they want to bet on and that should be registered as their bet number. I'm not sure the slice part is correct yet, but that's not the point - the function isn't executed at all even though I call it in the code. The debugger also just skips over it entirely when I try to step into the next line (tried with breakpoints etc).

Any idea why this happens and how it could be fixed please?

7
  • Show us where you call it, please. Commented Feb 5, 2013 at 10:37
  • 1
    The code above within the bet function will attach a click event to the item when clicked - that function still needs to be executed - but something needs to call bet() to attach the function to the click event on your table image. Commented Feb 5, 2013 at 10:37
  • You probably need to wire up the click event in the ready() function. Commented Feb 5, 2013 at 10:38
  • can you show us how you are calling this function? Commented Feb 5, 2013 at 10:39
  • 1
    Actually I'm quite sure that you misused slice() - this is a single DOM element, and you seem to expect yourBetNumber to be a string? Commented Feb 5, 2013 at 10:39

2 Answers 2

1

You need to call bet() within the document.ready Try this

$('document').ready(function () {
    bet();
});
Sign up to request clarification or add additional context in comments.

3 Comments

this is a strange notation you are using! that code won't work the way it is written now!
Yeah the brackets are wrong... still, it doesn't work even with correct notation because the whole thing is inside a game() function which is called when you click a Play button... yourBetNumber, a variable inside bet() is defined as a "blank" variable inside game() so calling bet() outside game() when the document is ready doesn't do anything.
Try to assign some id to the image you are using and assign that id to yourBetNumber instead of using slice. Hope this helps
0
function bet(el) {
    var yourBetNumber = $(el).slice(0, -1);
    $('#item' + yourBetNumber).clone().appendTo('#yourbet');
    $('table img').hide();
}

//Setting the CSS
$(document).ready(function () {
    $('table img').css('cursor', 'pointer');
});

//AddEventHandler
$('table img').click(function () {
    bet($(this)); //Call Function "bet" with the jQuery-Object
});

4 Comments

Thank you, I tried this but the click event is still not working. There is a function called right after the click event /choosing item/ is supposed to happen, could it be that I need some kind of code to "hold" that next function from being called until an item is clicked?
Do you have an URL for us?
Huh, the whole code is a lot... I copied it all here jsfiddle.net/yPt5U/2 , relevant function is at line 129, but the images aren't showing up obviously, if you think you need those to debug please let me know and I'll upload them somewhere (but you might just see the error with the code without them). This is the whole code btw, the bulk of it won't be relevant after the betting function. And jsfiddle messed up the formatting, but never mind. Thanks for taking a look if you do!
Try this: jsfiddle.net/Pisi2012/yPt5U/3 There were some issues with your selectors. I've put the edited part of the script in the top.

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.