Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here's my JavaScript:

function privacy(current)
{
    var $this = $(current), 
    $scope = $this.closest('.uk-button-group'), 
    value = $this.val();
    $.ajax({
        type: 'post',
        url: '/privacy.php',
        data: {key: value },
        success: function () {
        }
    });
}
$(function() {
    $("#privacy1, #privacy2, #privacy3").click(function() {
        privacy($this);
        alert('A button has been clicked!');
    });
});

And here's my HTML:

<div class="uk-button-group">
    <button class="uk-button" id="privacy1" value="1">Public</button>
    <button class="uk-button" id="privacy2" value="2">Protected</button>
    <button class="uk-button" id="privacy3" value="3">Private</button>
</div>    

When I click on one of the buttons, it should call the privacy function and alert me that a button has been clicked but it doesn't. Can anyone help me and show me what's wrong with my code? Much appreciated thank you!

share|improve this question
1  
Opening the console would have shown you where the error is... –  dystroy Dec 18 '13 at 10:42
1  
tip You can use the common class to bind to shorten the code Ex: $('.uk-button').click –  Anton Dec 18 '13 at 10:42
1  
privacy($this); $this is undefined, it's time to start using your console... –  A. Wolff Dec 18 '13 at 10:43
    
Sorry, I'm kinda new to javascript, I'm doing my code in notepad, could you please tell me where the console is located? Thanks –  Sieu Phan Dec 18 '13 at 10:43
1  
@SieuPhan console is in browser, press F12 to open it –  A. Wolff Dec 18 '13 at 10:43

4 Answers 4

up vote 4 down vote accepted

You do not have any identifier $this, you probably need to change $this with $(this) or simply this to pass the event source object

privacy(this);

OR

privacy($(this));
share|improve this answer
6  
Or just this as it is placed in a jQuery object within the function –  George Dec 18 '13 at 10:42
    
Thank you! I just realized I that it was this instead of $this which was undefined. Cheers –  Sieu Phan Dec 18 '13 at 10:46
    
You are welcome. –  Adil Dec 18 '13 at 11:00

Replace this :-

privacy(this);
share|improve this answer

Try here: http://jsfiddle.net/pqdgT/

$(function() {
    $("#privacy1, #privacy2, #privacy3").click(function() {
        privacy($(this));
        alert('A button has been clicked!');
    });
});
share|improve this answer

Here is your working code

And here is the refined code

function privacy(current)
{
    var btn = $(current), 
    scope = $(btn).closest('.uk-button-group'), 
    value = $(btn).val();
    $.ajax({
        type: 'post',
        url: '/privacy.php',
        data: {key: value },
        success: function () {
        }
    });
}
$(function() {
    $("#privacy1, #privacy2, #privacy3").click(function() {
        privacy(this);
        alert('A button has been clicked!');
    });
});

Please avoid reserved keywords while naming variables - Here is the list of reserved words

And be careful with your syntax and feel free to use the browser console.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.