Get_element value for checkbox
-
Hi all,
Im trying to get the state of a checkbox from a webdialog, but it seems that ruby thinks the checkbox is always on.
Here is some simple test code:
test.rb
def testing @@testwindow = UI::WebDialog.new("test", false, "test", 200, 200, 200, 200, true) html_path = Sketchup.find_support_file "test.htm" ,"Plugins" @@testwindow.set_file(html_path) @@testwindow.show end
test.htm
<html> <body text="FFFFFF"> <input type="checkbox" id="test" checked > </body> </html>
now when you run testing in sketchup, and the window shows up, try:
@@testwindow.get_element_value("test")
now turn the checkbox off and try it again.What am i missing here?
Kind regards,
Quintus Verburg -
what is the value it returns?
I'm not quite sure, but...
checkbox values are a bit quirky crossbrowsers - might need some help of a javascript to determine the state and return a corresponding value.But knowing the return value you get from
.get_element_value
will help. -
@@testwindow.get_element_value("test") on @@testwindow.get_element_value("test") on
first one is with checkbox checked, second one without ckeckbox checked......
-
This page might be of use: http://www.quirksmode.org/js/forms.html#scheckbox
I do have a suspicion that checkboxes doesn't report a value that reflect the checked state. So you might want to make a JS function that reads the checked state of the checkbox and put it into a hidden input field.so you first call that JS script using
.execute_script
- giving the JS function the element id as argument. then read the value of the hidden input field. -
Great idea!
Thanks for your helpfull and quick responses -
or maybe like this
- ruby
command = "_getcheckboxvalue()" webdialog.execute_script(command)
- javascript
function _getcheckboxvalue(){ //get the checkboxvalue thevalue=document.getElementById('test').value; //send it back to ruby query = 'skp;_sendcheckboxvalue@'+thevalue; window.location.href = query; }
3.ruby
webdialog.add_action_callback("_sendcheckboxvalue") {|webdialog,p| //p=the checkbox value //do something }
(untested)
-
The reason why I didn't use
.add_action_callback
calling back from JS to Ruby is that the amount of data might be limited and there are OSX issues with JS to Ruby callbacks. -
Hmm this is strange.
When I try to get the checkbox value in the javascript, it's again always on.
The hidden textbox method also doesn't work because I can't seem to set the textbox value. (and if I could, the checkbox would say its always on....)Any ideas?
-
In regard to the checkbox value - did you read my link?
-
@thomthom said:
In regard to the checkbox value - did you read my link?
Yes I did thank you for that. Played with it for 2 hours but couldn't manage to read the checkbox values in that manner.
The only thing that worked was the getElementById but it still reports the checkbox as on when its not. -
You might just try and keep the state of the control in your own Js variable, and then use onclick or onchange events to toggle the variable ON/OFF value.
I do it for 'toggle buttons' such as buttons that collapse / uncollapse a <DIV>.
Advertisement