views:

36

answers:

3

How do I send a javascript variable to a subsequent jquery function? Here is my code.

    <script type="text/javascript">
$(function() {
    var name = $("#name"),
        email = $("#email"),
        password = $("#password"),
        itemid = $("#itemid"),
        tips = $(".validateTips");

    function updateTips(t) {
        tips
            .text(t)
            .addClass('ui-state-highlight');
        setTimeout(function() {
            tips.removeClass('ui-state-highlight', 1500);
        }, 500);
    }

    $("#dialog-form").dialog({

        autoOpen: false,
        height: 320,
        width: 350,
        modal: true,
    /*
        buttons: {
            'Change category': function() {
            alert("The itemid2 is "+itemid2);

                var bValid = true;
                $('#users tbody').append('<tr>' +
                '<td>' + name.val() + '</td>' + 
                '<td>' + email.val() + '</td>' + 
                '<td>' + password.val() + '</td>' +
                '<td>' + itemid.val() + '</td>' +
                '</tr>'); 

                $(this).dialog('close');

            },
            Cancel: function() {
                $(this).dialog('close');
            }
        },
        */
        close: function() {
            allFields.val('').removeClass('ui-state-error');
        }
     });



    $('.changecategory')
        .button()
        .click(function() {
              var categoryid = $(this).attr("categoryid");
              var itemid = $(this).attr("itemid");
              var itemid2 = $(this).attr("itemid");
              var itemtitle = $(this).attr("itemtitle");
              var parenttag = $(this).parent().get(0).tagName;
              var removediv = "itemid_" +itemid;
         alert("The itemid is "+itemid);

            $('#dialog-form').dialog('open');




        });

});
</script>

I'll break it down.

  1. The .changecategory section happens FIRST when an image on my page is clicked.
  2. $("#dialog-form").dialog({ is then called, and the variable item id is not passed to this function. How can I pass a variable from one function to another? Is that possible.

Is there a way I can pass a variable to another jquery function without having to resort of setting a cookie with javascript and then using jquery to read it?

A: 

Nest the whole $("#dialog-form").dialog({ inside the click event of $('.changecategory')

You'll then be able to refer to the itemid inside the dialog.

Brant
As far as I can see, he does call `dialog('open')` inside the click event...
Robert Koritnik
A: 

You can simulate global variables by using properties of the window object: window.itemid = $('#itemid'); (Actually, variables that are not defined in a function scope are properties of window.)

You can also assign values to DOM objects with the data() function of jQuery: $('#dialog-form').data('itemid', itemid); in the click handler, then var itemid = $('#dialog-form').data('itemid'); in the dialog.

Tgr
A: 

Your dialog('open') call won't be able to see any of your itemid variables you defined because they're in a completely different closure.

If you'd tell us which dialog plugin you're using we could help you out a bit. If it's jQuery UI Dialog widget then I suggest you do this:

$('.changecategory')
    .button()
    .click(function() {
        var scope = $(this); // for the sake of code speed
        var categoryid = scope.attr("categoryid");
        var itemid = scope.attr("itemid");
        var itemid2 = scope.attr("itemid");
        var itemtitle = scope.attr("itemtitle");
        var parenttag = scope.parent().get(0).tagName;
        var removediv = "itemid_" +itemid;
        alert("The itemid is "+itemid);
        $('#dialog-form').dialog('option', 'itemId', itemId); // store the id
        $('#dialog-form').dialog('open');
    });

So before you open the dialog, you can set some dialog's internal option that will be available in the closure of the open function as dialog's option.

The other (non-recommended way) would be to use @Tgr's approach with globals.

Robert Koritnik
I tried it and can't get it to work.Look for the lines where it says //NEW LINE ADDEDCheckout http://codedumper.com/odute and reply please.
desbest