Adrian
Adrian

Reputation: 2012

shorthand if else mixing php with javascript not working

Im attempting to add a checked attribute in an input field in javascript that will be outputted as html

Here is my line..

var checked = ( <?php echo $original_delivery_date; ?> == json['dates_per_zone'][i]) ? "checked" : "";

I have verified that both json['dates_per_zone'][i] and $original_delivery_date are correct and displaying, but nothing is appearing.

Is that the correct syntax?

My final line is then...

html += '<input ' + checked + ' name="delivery_date" value="' + json['dates_per_zone'][i] + '">

Upvotes: 0

Views: 63

Answers (2)

Hassan Sardar
Hassan Sardar

Reputation: 4523

Use Date as a string: Like this:

var checked = ('<?php echo $original_delivery_date; ?>' == json['dates_per_zone'][i] ? "checked" : "" );

Upvotes: 0

insertusernamehere
insertusernamehere

Reputation: 23590

It should be:

var checked = ( '<?php echo $original_delivery_date; ?>' == json['dates_per_zone'][i] ? "checked" : "" );

You're missing the quotes around the string that PHP prints.

Upvotes: 1

Related Questions