Adam
Adam

Reputation: 6132

How to print "" if value of variable is null?

I'm rendering some HTML in which I inject the value of a textbox with javascript. See: {$T.blob.title}

<input type="text" maxlength="40" size="16" id='imgtitle_{$T.blob.Id}' value='{$T.blob.title}' class="imagetitleinput" /><br />

However, this string may be null so the textbox shows the value "null" to the visitor. How can I check (preferably inline) if the value of {$T.blob.title} is null and if so set the value of this textbox to ""?

I'm using this template engine: http://jtemplates.tpython.com/

Upvotes: 1

Views: 3624

Answers (2)

Debasish Chowdhury
Debasish Chowdhury

Reputation: 188

This should work [Using Jquery]

$(document).ready(function () {
    $(".imagetitleinput").each(function () {
        if($.trim($(this).val()) == '') {
            $(this).val('NULL');
        }
    });
}) 

Though not sure if you want to use Jquery here or not.

Upvotes: 1

HankHendrix
HankHendrix

Reputation: 295

You need to perform this logic before your $T.blob.title variable even gets near entering the DOM. Don't start performing inline JS inside an element - it's just not good practise!

I assume you are building an object literal further up the flow? If so, check the value of $.blob.title and if it equals null then set the value to an empty string.

Upvotes: 4

Related Questions