Sasha
Sasha

Reputation: 8705

JQuery - add value to the hidden input

How can add value to the hidden input filed? In this case input is being created as variable, and latter on inserted into the DOM if needed. The code:

<script>
    var forma = $('form#mali_oglas'),
    pomoc = $('div[role=pomoc]'),
    div = $('.mali_oglas_pomoc'),
    input = forma.find('input, textarea'),
    code = forma.find('#code')
    time_input = '<input type="hidden" name="time" />',
    paid_input = '<input type="hidden" name="paid" value="1" />';

    $('textarea').autosize();

    input.on('click', function(e){
        var name = $(':input:focus').attr("name");
        pomoc.fadeOut('slow').promise().done( function(){
         div.find("[data-pomoc='" + name + "']").fadeIn('slow');
     });
    });


    code.on('focusout', function(){
        var url = '<?php echo base_url() ?>global_info/gi_get_payment_code',
        kod = $(this).val();
if ($('input[name=paid]').length != 1)
    {
        $.post(url, {code : kod}, function(data){
            console.log(kod);
            $('.mali_oglas_img_paid').slideDown('slow');
            code.next('.mali_oglas_greska').remove();
            code.after(time_input.val(data) + paid_input)
            .css({'border' : '1px solid rgba(34,139,34,.5)'});
        });
    };
    });

    code.ajaxError(function() {
  $(this).next('.mali_oglas_greska').remove();
  $(this).after('<p class=mali_oglas_greska>Uneti kod je pogresan.</p>').css({'border' : '1px solid rgba(255,0,0,.5)'});
});
</script>

Upvotes: 1

Views: 1462

Answers (3)

Ram
Ram

Reputation: 144739

You are calling the val method for a string, you should first create a jQuery object, change:

time_input.val(data)

to:

$(time_input).val(data)

Note that you cannot concatenate an object with a string, you should call the after method 2 times or use add method.

Upvotes: 2

charlietfl
charlietfl

Reputation: 171698

You are trying to use a jQuery method on a variable that is simply an html string

You can convert the variable to a jQuery object element wrapping it in $()

You can't concatenate the 2 inputs once one is an object so you would need to reverse the insertion

code.after( paid_input).after( $(time_input).val(data) )
/* should place "time_input" before "paid_input"

Upvotes: 1

Musa
Musa

Reputation: 97727

time_input and paid_input are strings not jQuery objects, try this instead

time_input = $('<input type="hidden" name="time" />'),
paid_input = $('<input type="hidden" name="paid" value="1" />');

Upvotes: 0

Related Questions