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 selectingmyElement
etc. – Robin Maben Commented Aug 16, 2012 at 6:25 -
1
If
myElement.checked = true
works in JavaScript thenmyElement.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
2 Answers
Reset to default 4UPDATE:
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条)