Software Developer
Software Developer

Reputation: 197

How do i pass the value from a variable to .html() using javascript

Hi i have this change function in jquery when selecting the time, the time will calculate to four hours. Upon selecting using change function the time and calculate for four hours works well. Now my problem is how will i able to pass the value to my .html()

Here is my script below:

 $('#changeTime').change(function() {
            $("#checkoutT").hide();
            var time = $(this).val();


            var timeArr = time.split(":");

            var timevar = timeArr[0];

            var x = <?php echo $minStayExp[0] ?>;
            var calc = parseInt(timevar) + parseInt(x);

            console.log(timevar);
            console.log(calc);
            alert(calc);


            $("#checkoutTime").html('<p class="text-sm text-muted"></p>');


         });

Now i want my value in my var calc will passed to .html() line of code in here:

$("#checkoutTime").html('<p class="text-sm text-muted"> + calc +</p>');

Something like this.

Can someone help me figured this thing out? Any help is muchly appreciated. TIA

Upvotes: 0

Views: 59

Answers (3)

aleks.ko
aleks.ko

Reputation: 50

There was an error in your concatenation

'<p class="text-sm text-muted">' + calc + '</p>

Upvotes: 0

Diogo DA.
Diogo DA.

Reputation: 71

You need to concat.

$("#checkoutTime").html('<p class="text-sm text-muted">' + calc +'</p>');

Upvotes: 4

Willy
Willy

Reputation: 3824

You can use string interpolation:

$("#checkoutTime").html(`<p class="text-sm text-muted">${calc}</p>`);

Upvotes: 4

Related Questions