javascript - Programmatically check a checkbox using CoffeeScript - Stack Overflow

How can I programmatically check a checkbox in Coffeescript ?I know in Javascript, I can use this :myEl

How can I programmatically check a checkbox in Coffeescript ?

I know in Javascript, I can use this :

myElement.checked = true

Can I do something like the following in Coffeescript ?

myElement.checked "true"

I have already tried the above but it isn't working for me.

Any help in this regard will be highly appreciated. Thanks.

How can I programmatically check a checkbox in Coffeescript ?

I know in Javascript, I can use this :

myElement.checked = true

Can I do something like the following in Coffeescript ?

myElement.checked "true"

I have already tried the above but it isn't working for me.

Any help in this regard will be highly appreciated. Thanks.

Share Improve this question asked Aug 16, 2012 at 6:09 MyxticMyxtic 5,6995 gold badges50 silver badges69 bronze badges 4
  • 1 Is myElement a checkbox? I suspect something else is wrong. You could post some relevant code. Eg: When are you trying to call this? How are you selecting myElement etc. – Robin Maben Commented Aug 16, 2012 at 6:25
  • 1 If myElement.checked = true works in JavaScript then myElement.checked = true should also work in CoffeeScript. – mu is too short Commented Aug 16, 2012 at 6:28
  • Yes, myElement is a checkbox. I am selecting it using it's ID. Also, myElement.disable "true" is disabling the checkbox absolutely fine in CoffeeScript. And so it seems there's no problem with the selection of the element. – Myxtic Commented Aug 16, 2012 at 6:40
  • @muistooshort: You're right, myElement.checked = "yes" works perfectly in plain javascript. – Robin Maben Commented Aug 16, 2012 at 6:51
Add a ment  | 

2 Answers 2

Reset to default 4

UPDATE:

myElement.checked = "yes";

myElement.checked = null;// for unchecking

So, for your case,

a.checked = b.checked; //i.e. check a if b is checked

Example here


//CoffeeScript
a = {}
a.b = 10
alert a.b

Compiles to..

//javascript
var a;
a = {};
a.b = 10;
alert(a.b);

However, a.b 10 piles to a.b(10) in javascript, which is not what you want. :)

Your myElement is jQuery object and you want to address DOM element:

// HTML
<input class="toggle" type="checkbox"/>


//coffeescript

$('.toggle')[0].checked = true

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742383635a4433614.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信