Vince
Vince

Reputation: 1535

How to assign a JQuery variable value to a Laravel Blade input

I have a variable(string) called "cost". If I attempt to assign it to a

<p class="cost"> </p> 

it works fine. However when I attempt to assigned it to Blade generated "readonly" input I get nothing. I get no output of the "cost" value in the input field. I have tried unescaping the BLade input, also tried raw JS.

  $(".quantity").on('change', function(){
    var quantity = parseInt($(this).val());
    var num = (price * quantity);
    var cost = num.toString();
    $('.cost').html(cost);  // This will not assign the value ! 
});

Blade Input:

<div class="col-sm-2 col-md-2">{!! Form::text('cost[0]', null ,['class'=>'form-control cost', 'readonly'=>'readonly' ]) !!}</div>

The source code Blade produces looks like this:

<div class="col-sm-2 col-md-2"><input class="form-control cost" readonly="readonly" name="cost[0]" type="text" value=""></div>

Upvotes: 0

Views: 6527

Answers (1)

charlietfl
charlietfl

Reputation: 171679

You aren't using class="cost" on input you are using id="input" so selector needs to be changed.

In addition you set value not html on inputs

Try

$('#cost').val(cost);

Upvotes: 3

Related Questions