Skipping jQuery / javascript

Until now, I haven’t done much in javascript. I recently started playing with jQuery and stumbled upon a tutorial on how to make a "Change in Place" form.

The resulting code works fine if you hardcode the id of your container div into a function, but I am having problems generalizing it to the identifier passed to the function.

The code starts as follows:

$(document).ready(function() {
    setClickable($('#editInPlace'));
});

function setClickable(target)
{
    $(target).click
    (
        function()
        {
            //Do some dom manipulation

            $('.buttonA').click(function() { saveChanges(this, false); });
            $('.buttonB').click(function() { saveChanges(this, true); });
        }
    )

    .mouseover(function() { $(this).addClass("editable"); })
    .mouseout(function() { $(this).removeClass("editable"); });
}; //end of function setClickable

function saveChanges(obj, cancel)
{
    //Handle the save changes code

    //Reset the div
    $(obj).parent().after('<div id="editInPlace">' + t + '</div>').remove();

    //Call setClickable
    setClickable($('#editInPlace'));
}

In the part that I came across, there is the last call setClickableinside the function saveChanges. If I hardcode the value $('#editInPlace'), it works fine. However, if I change the function signature to

function saveChanges(target, obj, cancel)

and pass the target from the setClickable method:

$('.buttonB').click(function() { saveChanges(target, this, true); });

call

setClickable(target);

doesn't seem to work, and I can't figure out why.

+3
2

:

function setClickable(target)
{
    $(target).click
    (

function setClickable(target)
{
    target.click
    (

( jQuery), . . :

$(document).ready(function() {
    setClickable('editInPlace');
});

function setClickable(id)
{
    $("#" + id).click
    (

, ?

+6

setClickable ($ (target)); , jQuery.

+1

Source: https://habr.com/ru/post/1705713/


All Articles