I have tasks in my app with boolean attribute 'completed'. I would like to be able to check the checkbox and to instantly change that boolean attribute from false to true ( and not have to click the submit button ). How to do that?
1 Answer
You could try:
<script>
function toggle(id, value)
{
document.getElementById('img').src = "scriptURL?id=" + id+ "&value=" + value; }
</script>
<input type="checkbox" name="toggleBox" onclick="toggle(this.id,this.checked);"/>
<img src="scriptURL" id="img" style="display:none;"/>
The image is hidden, when you click the checkbox it sends a message to the image to re-load from "scriptURL".
"ScriptURL" recieves the name of the control and the value - your code can pick these up from the query string and process them.
It's completely transparent to the user and doesn't need any additional frameworks.
Or
<script>
function toggle(id, value)
{
var url = "scriptURL?id=" + id+ "&value=" + value;
// this sends the name and value parameters to the scriptURL
$("<div/>").html(url);
}
$(document).ready(function(){
$(".toggleBox").bind("click", function(){
toggle($(this).attr("id"), $(this).is(':checked'));
});
});
</script>
<input type="checkbox" id="something" name="something" class="toggleBox" />
Ruby script:
...
require 'cgi'
params = CGI.parse(request.query_string)
# params is now {"id"=>["id name"], "value"=>["true or false"]}
**Completed = p['value'].first**
... rest of your code ...
2 Comments
oFca
ok, but I don't understand what you wrote. Can you please clarify or point me to some noob-friendly AJAX with Rails tutorial?
oFca
I posted the question similar to this but I got further in my travels... :) if you'd be so kind and look at it stackoverflow.com/questions/10656860/…