Ask coding questions

← Back to all posts
How to put the if statement in HTML
FrankGomezMonta (0)

I am using the Chrome browser and how do I put in the 'if' statement in HTML?

eankeen (606)


yeah, so nearly all html markup you write will automatically get rendered and displayed on the page, so there really isnt anything like an 'if statement in HTML'

if you want a more dynamic page, youre gonna have to use javascript (or css, depending on what you want to do).

i think you may be looking for something along the lines of a 'templating engine'. some examples of implementations include handlebars or mustache. it would look something like this:

<div class="entry">
  {{#if author}}
    <h1>{{firstName}} {{lastName}}</h1>

however, none of this syntax is valid html, so you cant just use this as-is. you would have to use some templating engine library (like handlebars or mustache, that i previously mentioned)

i would lookup 'client side templating engines' using some search engine to get more information. you can also use them server side with something like nodejs, but that requires a bit more setup.

hope i could help :)

mwilki7 (194)

As far as I know HTML by itself does not have if-else statements (I'm googling this right now so I'm probably wrong).
It's easiest to add this sort of logic in JavaScript which I know for a fact has if-else statements and all that other stuff.

What do you want your HTML to do with if statements?