0

I would hide an html element when a condition occurs. I try to implement this code in coffeescript:

if byName == username 
document.#prv-btn.style.display = 'none'

I have already tried this code but don't run. The element #prv-btn is my html element. In my page i have some users and for each of them i have this #prv-btn. For example if i have ten users, i have ten #prv-btn, but only one i want that i see, each user see the button near his name. How can I do?

1 Answer 1

3

There are a couple of issues:

  1. You need to indent the body of if clauses in CoffeeScript.
  2. You need to use getElementById() to actually select the button by its ID.

Also, I recommend using jQuery for DOM work such as this. It works just fine with the compiled CoffeeScript.

Code:

if byName == username 
    document.getElementById("prv-btn").style.display = 'none'

Here is a link to a jsFiddle that I made for this: http://jsfiddle.net/jonathanporta/tw3nn/1/

Sign up to request clarification or add additional context in comments.

3 Comments

And if i have a button in a div, where the div id change but the button id doesn't change, how can i do? 'document.getElementById("mydiv").getElementById("prv-btn").style.display = 'none'' ??
You should not use the same ID on multiple buttons. Use a class for that. You should us jQuery if you want to do more than select by ID.
Also look at querySelector

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.