1

I'm wondering how is possible to call a function with parameters inside a method. I have 2 functions and i'd like to call function deleteCode() when clicked on list element which is created by addCode() function. I'm sure the solution is really simple, but i just can't see it right now. Many thanks!

function addCode(code) {
    $('#codeList').append('<li class="codeList" onClick="deleteCode(code);">' + code + '</li>');
}

function deleteCode(code) {
    $('#'+code).remove();
}

4 Answers 4

3

Do it unobtrusive and you're fine.

function addCode(code) { 
    $('#codeList').append($('<li>', {
        'class':    'codeList',
        'text':     code,
        'click':    function(e) {
             deleteCode(code);
        }
    }));
}

Ref.: $()

Sign up to request clarification or add additional context in comments.

Comments

0

Create the <li> element via code rather than appending raw HTML.

function addCode(code) {
    // Create the <li>
    var newEl = document.createElement("li");
    newEl.className = "codeList";

    // Assign the click function via jquery's event helper.
    $(newEl).click(function(code) {
        // Call your deleteCode function and pass in the given parameter.
        deleteCode(code);
    });

    // Append the new element to the codeList node.
    $(#codeList).append(newEl);
}

Comments

0

You can try:

function addCode(code) {
$('#codeList').append('<li class="codeList" onClick="deleteCode(' + code + ');">'+code+'</li>'); 
}

Comments

0

You can do that like this:

function addCode(code) {
    $('<li class="codeList">' + code + '</li>').click(function() {
        deleteCode(code);
    }).appendTo('#codeList');
}

function deleteCode(code) {
    $('#'+code).remove();
}

...or more simply:

function addCode(code) {
    $('<li class="codeList">' + code + '</li>').click(function() {
        $('#'+code).remove();
    }).appendTo('#codeList');
}

When using a library like jQuery (or even when not, frankly), there's virtually never any reason to use the old-style onclick attributes for setting up handlers. In the above, I've replaced it with the click function, which sets up a handler when the user clicks the element.

Note: Lazarus notes that your code is removing an element by id using the code value:

$('#' + code).remove();

...but that the code doesn't produce an element with that ID. I assume you've added that element with some other code elsewhere, and that the goal isn't to remove the li you've added with this code.

If you did want to remove that same li on click, no need for an ID at all:

function addCode(code) {
    $('<li class="codeList">' + code + '</li>').click(function() {
        $(this).remove(); // <== Changed here
    }).appendTo('#codeList');
}

4 Comments

+1, what I was typing. @jAndy's answer is another great way of doing it. @Downvoter, it's just bad form not to say why you've down voted.
I wasn't the downvoter but I might suspect that the lack of a control ID that would cause the example to fail to operate might be the reason for the downvote. You can't $('#'+code).remove(); if there's no ID set to code to remove.
@Lazarus: Thanks. That logic just comes from the OP's code. For all we know, he is adding an element with that ID which this code is then supposed to remove, and it's just not in the code he quoted. (We don't know that clicking the li is supposed to remove it -- that can be done rather more directly!) In any case, that would be inappropriately harsh, esp. with no comment.
@T.J. Crowder: The intent seems clear from the sample but accepted that it's probably an equal stretch to either assumption.

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.