Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery clone an input doesn't see changes to the value

I have a form with hidden inputs.

I .clone() them and show them to the user in a .dialog().

The user makes some changes and i use .val() to change the hidden fields.

However the next time i clone the form(without reloading the page) i have the initial values again, and never the updates ones.

There seems to be this weird bug/result? see http://jsfiddle.net/YvBfP/ (broken for visible input too)

$(this).closest('td').find('button').click( function ()
{
    var d = $('#pagamento_anticipato').html();

    $(d).dialog({
        modal: true,
        width: 400,
        height: 300,
        close: function( event, ui ) {
                var importo = $(this).find('input[type="text"]').val();
                var descrizione = $(this).find('textarea').val();
                var select = $(this).find('select').val();
                $(this).remove();

                $('#pagamento_anticipato').find('input[id="importo"]').val( importo );
                $('#pagamento_anticipato').find('#descrizione').val( descrizione );
                $('#pagamento_anticipato').find('#tipo').find('option[value="' + select + '"]').attr('selected', true);

            }
    });
    return false;
});
like image 474
max4ever Avatar asked Dec 11 '22 19:12

max4ever


2 Answers

using .val() sets/gets the current value and not the attribute for the element.

$(element).val(value) // sets current value
$(element).val() // <-- will always return the current value

to change the attribute you have to use .attr()

$(element).attr('value',value)

Then you will see the change in the HTML

http://jsfiddle.net/wirey00/bJjjw/

EDIT:

Just found out this doesn't work with jQuery 1.5.x and lower.. tested it with jQuery 1.6.0+ and it worked fine

like image 69
wirey00 Avatar answered Dec 30 '22 13:12

wirey00


You are setting the value property of the input but checking to see if the value attribute has changed. To see the elements value use .val() don't check its html. However if you need to change the elements value attribute use setAttribute

$('#money')[0].setAttribute('value', 3000);

http://jsfiddle.net/YvBfP/4/

like image 33
Musa Avatar answered Dec 30 '22 11:12

Musa