Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: How can I assign a variable back to a hidden element in the document?

How can I assign a variable (button id that was clicked), to the jQuery Diaglog, then back to the hidden element (myhiddenid) in the document for later use?

<table border="0" class="time_data">
    <td><button type="button" id='001' class="del-fruit" > Apple </td>
    <td><button type="button" id='002' class="del-fruit" > Banana </td>
    <td><button type="button" id='003' class="del-fruit" > Cantalope </td>
</table>

<div id='myhiddenid' style="display:none;"></div>

<script type="text/javascript">
$("#dialog-form").dialog({
    autoOpen: false,
    height: 150,
    width: 350,
    modal: true,
    resizable: false,
    buttons: {
        'Yes': function() {
            var bValid = true;
            allFields.removeClass('ui-state-error');

            bValid = bValid 

            if (bValid) {
                //the assining of myhiddenid should be here!!
                //and should contain the ID of the button that i clicked.
                $(this).dialog('close');
            }
        },
        No: function() {
            $(this).dialog('close');
        }
    },
    close: function() {
        allFields.val('').removeClass('ui-state-error');
    }
});


$('.del-fruit')
    .button()
    .click(function() {
    $('#dialog-form')
        .dialog( "option", "title", this.id )
        .dialog('open');
});
</script>
like image 912
aldrin Avatar asked Nov 20 '25 22:11

aldrin


2 Answers

Instead of a div, you can use hidden field and assign it's value: For e.g.

HTML

<input type="hidden" id="hdnId" />

jQuery

$("#hdnId").val(id);
like image 177
Sang Suantak Avatar answered Nov 23 '25 12:11

Sang Suantak


Agreed with first answer, but if you want to use a hidden div:

set:

$("div#myhiddenid").html("whateverId");

get:

var foo = $("div#myhiddenid").html();
like image 31
Alec Avatar answered Nov 23 '25 11:11

Alec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!