0

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>
5
  • Sure it's possible. Where are you stuck? What have you tried? Commented Mar 23, 2014 at 23:01
  • And why are you using data- for one custom attribute, but not for the other? Commented Mar 23, 2014 at 23:02
  • 1
    And if all you want is to change its background, why not add a class and just do it with CSS? Even without the class, you can just use CSS. #calevvw[dhx_1_id=6669] {background-color: blue} Commented Mar 23, 2014 at 23:04
  • To make it easier, consider jquery, or better still knockout.js (though css is simpler in this case thanks @cookiemonster). Commented Mar 23, 2014 at 23:05
  • And you definitely don't need jQuery or any other library for that matter just for this tiny bit of JavaScript. Commented Mar 23, 2014 at 23:17

2 Answers 2

2
var el = document.getElementById('calevvw');
el.style.backgroundColor = +el.getAttribute('dhx_l_id') == 6669 ? 'blue' : 'white';
Sign up to request clarification or add additional context in comments.

2 Comments

Probably it's not necessary to use + to convert the attribute to a number, since I use == instead of the strict ===, but I don't like loose comparisons between different types.
@Steven The code you posted here has errors. See my answer. And do not post code in comments because it will be very hard to read it. Instead, edit your question and post code there.
1

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>

2 Comments

thats the same answer as above and does not work upon page load. or at any point.
@Steven No it is not the same. Your comment to that answer was that it didn't work and you posted code that you tried, but your code contained errors, so of course it wouldn't work. Here, I'm providing the entire HTML and JavaScript to make sure you're making mistakes when you use it. For example, the order of JavaScript bellow the link is important. I also added "Test" as the text of the link, otherwise you won't see the background. This code example is working and I tested it. If you're having issues applying it to your code, you need to edit your question and post your code so we can help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.