Steven
Steven

Reputation: 401

how to use an element id in javascript if statement

I have the below anchor tag with an ID tied to a record in my database. I want to do something in javascript to say if dhx_1_id = 6669 then make calevvw's backround blue else make it white. Is this possible? Thanks.

<a id="calevvw" dhx_l_id="6669" data-ajax="false" href="eventview.php?eventid=6669" class="dhx_list_item dhx_list_day_events_time" style="width:auto; border-bottom: 1px solid #cbcbcb; padding:px; margin:px;">
</a>

Upvotes: 0

Views: 1315

Answers (2)

Racil Hilan
Racil Hilan

Reputation: 25351

Your code has errors. Try this:

<a id="calevvw" dhx_l_id="6669" data-ajax="false" href="eventview.php?eventid=6669"
 class="dhx_list_item dhx_list_day_events_time"
 style="width:auto; border-bottom: 1px solid #cbcbcb; padding: 0px; margin: 0px;">Test</a>

<script type="text/javascript">
    var el = document.getElementById('calevvw');
    el.style.backgroundColor = el.getAttribute('dhx_l_id') == 6669 ? 'blue' : 'white';
</script>

Upvotes: 1

Oriol
Oriol

Reputation: 288080

var el = document.getElementById('calevvw');
el.style.backgroundColor = +el.getAttribute('dhx_l_id') == 6669 ? 'blue' : 'white';

Upvotes: 2

Related Questions