admin管理员组

文章数量:1417415

Is there any alternative to JS 'innerHTML' property in CoffeeScript?

In JS, you would end up writing something like:

document.getElementById('element').innerHTML = "blah_blah"

What is the alternative to do that in CoffeeScript? I have already tried:

element = $('element')
element.html "blah_blah"

Is there any alternative to JS 'innerHTML' property in CoffeeScript?

In JS, you would end up writing something like:

document.getElementById('element').innerHTML = "blah_blah"

What is the alternative to do that in CoffeeScript? I have already tried:

element = $('element')
element.html "blah_blah"
Share Improve this question edited Mar 18, 2015 at 17:45 Oleg 9,3692 gold badges45 silver badges59 bronze badges asked Mar 18, 2015 at 17:43 Rahul BhargavaRahul Bhargava 5481 gold badge7 silver badges17 bronze badges 6
  • 1 CoffeeScript simply provides a different syntax to write JS. It doesn't have anything to do with the DOM API. So I guess the answer to "Is there any alternative to JS 'innerHTML' property in CoffeeScript?" is no, because that's not what CoffeeScript does. The code you posted actually looks like you are using jQuery, but with the wrong selector. Should be $('#element'). – Felix Kling Commented Mar 18, 2015 at 17:46
  • So, you are saying that I need to do same old boring stuff again.. :) Something like: document.getElementById()... – Rahul Bhargava Commented Mar 18, 2015 at 17:47
  • See my updated ment. CoffeeScript: A "language" that piles to JS. jQuery: A JS library around the DOM API. Those are two unrelated and independent things. – Felix Kling Commented Mar 18, 2015 at 17:48
  • "I have already tried:" ... and what happened when you tried that? – JLRishe Commented Mar 18, 2015 at 17:51
  • Looks like its just a bug in your jQuery selector: jsfiddle/xf3kgkhv/1 – Jason Sperske Commented Mar 18, 2015 at 17:54
 |  Show 1 more ment

2 Answers 2

Reset to default 3

CoffeeScript doesn't replace or augment the DOM API. If you want to use some fancy syntax, then check out the examples below. They all do the same thing.

CoffeeScript (without jQuery):

document.getElementById('element').innerHTML = 'blah_blah'

(document.getElementById 'element').innerHTML = 'blah_blah'

document
  .getElementById 'element'
  .innerHTML = 'blah_blah'

CoffeeScript (with jQuery, note the # in the selector):

$('#element').html 'blah_blah'

($ '#element').html 'blah_blah'

$ '#element'
  .html 'blah_blah'

There is actually an alternative to do that in coffee script.

password        = $('#user_password')
message.html "Password Match"

I missed '#' selector tag in my question.

本文标签: javascriptSet innerHTML in CoffeeScriptStack Overflow