user2715898
user2715898

Reputation: 1011

javascript: display a <div> block only if a variable has certain value

I want to display a <div> section in my html file only if a variable(passed from a django view) equals "abc". I sort of think that javascript can be of help here but not sure how- new to both. How can this be achieved?

Upvotes: 0

Views: 1213

Answers (2)

Asons
Asons

Reputation: 87251

If you want to be able to toggle this div, you could use the variable to set an attribute value, and with an attribute selector, use CSS to display it.

Maybe something like this

<div data-showme="{% if var == 'abc' %}true{% endif %}">Hi there</div>

Stack snippet

div[data-showme] {
  display: none;
}

div[data-showme='true'] {
  display: block;
}
<div data-showme="true">Hi there</div>

<div data-showme="">Hi there back (hidden)</div>

Upvotes: 1

Federico klez Culloca
Federico klez Culloca

Reputation: 27139

You can do this directly from the template without involving javascript on the client.

{% if var == 'abc' %}
     <div>Rendered!</div>
{% endif %}

Upvotes: 3

Related Questions