How to check/uncheck dojo radio button from javascript

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

How to check/uncheck dojo radio button from javascript

Tushar
This post has NOT been accepted by the mailing list yet.
Hi All,

I am referring to Dojo 1.1.3 liabrary. i can get the status of radio button in javascript but not able to set radio button checked / unchecked. can anyone please help me!

Note:
                        if (document.getElementById(radioOne).checked == true){
                            alert("radio is checked ");                                              // is working  
                            document.getElementById(i).checked = true;                 // is not working  
                        }
Reply | Threaded
Open this post in threaded view
|

Re: How to check/uncheck dojo radio button from javascript

Simon Speich
Did you create the radio with dijit? Then it would be dijit.byId. From your code example it seems you are not using dojo at all. If you are accessing a normal form radio, you could use dojo.byId instead of document.getElementById, which is not supported by IE6.

Anyway is this example real code? Because then it doesn't work since you are using radioOne (is that a variable, otherwise you should use a string "radioOne") and then i instead of radioOne. You could also try "checked" instead of true.
Reply | Threaded
Open this post in threaded view
|

Re: How to check/uncheck dojo radio button from javascript

Tushar
This post has NOT been accepted by the mailing list yet.
Hi Simon,

I tryied options suggested by you, but not wored out :-(
Actually I am trying to set next Radio on click of DojoButton. PFB code.

<html>
<head>
<style type="text/css"> @import "dojoroot/dijit/themes/tundra/tundra.css"; @import "dojoroot/dojo/resources/dojo.css" </style>                <script type="text/javascript" src="dojoroot/dojo/dojo.js" djConfig="parseOnLoad: true"></script>
    <script type="text/javascript" src="dojoroot/dojo/dojo.xd.js" djConfig="parseOnLoad: true"></script>   
        <script type="text/javascript"> dojo.require("dojo.parser"); dojo.require("dijit.form.Button"); dojo.require("dijit.form.RadioButton"); </script>
<script type="text/javascript"> function set_next_Radio() { var radioLength = document.radioObj.r1.length; for(var i = 1; i <= radioLength; i++) { if (document.getElementById(i).checked == true) { alert("checked "+i); i++; //document.radioObj.r1[i].checked = true; //error document.getElementById(i).checked = true; //not working //document.getElementById(i).checked = checked; //error //dojo.byId("r1[i]").checked = true; //error //dojo.byId(r1[i]).checked = true; //error //dijit.byId("r1[i]").checked = true; //error //dijit.byId(r1[i]).checked = true; //error } } } </script>
</head>
<body class="tundra">
<div>
<form name="radioObj">
 <input type="radio" dojoType="dijit.form.RadioButton" name="r1" value="1" id="1" checked=1/> <input type="radio" dojoType="dijit.form.RadioButton" name="r1" value="2" id="2" /> <input type="radio" dojoType="dijit.form.RadioButton" name="r1" value="3" id="3" /> <input type="radio" dojoType="dijit.form.RadioButton" name="r1" value="4" id="4" /> <button dojoType="dijit.form.Button" type="button" id="b1" value="next" onClick="set_next_Radio();"> > </button>
  </form>
</div>
</body>
</html>

Reply | Threaded
Open this post in threaded view
|

Re: How to check/uncheck dojo radio button from javascript

Simon Speich
A number of things:
- checked has to be a string: ..checked = 'checked'
- for dijits you need to use dijit.byId, dojo.byId won't work
- you need to set dijit attributes in dojo 1.5 with dijit.get/set or in  dojo 1.4 with attr. In older ones I don't know

see http://dojotoolkit.org/reference-guide/dijit/_Widget.html

dijit.byId("r1[i]").set('checked', true);
dijit.byId(r1[i]).set('checked', 'checked');
Reply | Threaded
Open this post in threaded view
|

Re: How to check/uncheck dojo radio button from javascript

Tushar
This post has NOT been accepted by the mailing list yet.
Hi Simon,

I don't get anything :(

Can you please elaborate?
or otherwise it would be great if you could update in the code I posted earlier..

Thanks again,

Tushar
Reply | Threaded
Open this post in threaded view
|

Re: How to check/uncheck dojo radio button from javascript

neekfenwick
Some points.  Don't feel you have to respond to each one.

1/ document.getElementById() should be done using dojo.byId() in dojo.
2/ use dijit.byId(x) to get a reference to the dijit object for the element with id x.  
3/ versions of dijit below 1.4 should use dijit.attr(x, y) to set attribute x to value y.
4/ don't include both dojo.js and dojo.xd.js .. they're two versions of the same thing for use in completely different situations.
5/ usually better to use console.log because it doesn't interrupt the flow of execution, can log objects, and can log several statements one after the other, rather than each one disappearing when you click OK on the alert window.
6/ why on earth use such an old version of dojo? it looks like you're learning how to use dojo, not maintaining a legacy app.. so use the latest stable release, 1.5.0.

Try this code.  I've not tested it, just hacked your post around until it looks more sane in places.  Let us know if this clarifies things for you.

<html>
<head>
<style type="text/css"> @import "dojoroot/dijit/themes/tundra/tundra.css"; @import "dojoroot/dojo/resources/dojo.css" </style>                <script type="text/javascript" src="dojoroot/dojo/dojo.js" djConfig="parseOnLoad: true"></script>
        <script type="text/javascript"> dojo.require("dijit.form.Button"); dojo.require("dijit.form.RadioButton"); </script>
<script type="text/javascript"> function set_next_Radio() { var radioLength = document.radioObj.r1.length; for(var i = 1; i <= radioLength; i++) { var radioButton = dijit.byId("r" + i); console.log("resolved ", i, " to ", radioButton); if (radioButton && radioButton.attr('checked') == true) { console.log("radioButton ", radioButton, " is checked"); radioButton.attr('checked', false); } else { console.log("radioButton number " + i + " not found."); } } } </script>
</head>
<body class="tundra">
<div>
<form name="radioObj">
 <input type="radio" dojoType="dijit.form.RadioButton" name="r1" value="1" id="r1" checked=1/> <input type="radio" dojoType="dijit.form.RadioButton" name="r1" value="2" id="r2" /> <input type="radio" dojoType="dijit.form.RadioButton" name="r1" value="3" id="r3" /> <input type="radio" dojoType="dijit.form.RadioButton" name="r1" value="4" id="r4" /> <button dojoType="dijit.form.Button" type="button" id="b1" value="next" onClick="set_next_Radio();"> > </button>
</form>
</div>
</body>
</html> 
Reply | Threaded
Open this post in threaded view
|

Re: How to check/uncheck dojo radio button from javascript

BigBlueOcean
This post has NOT been accepted by the mailing list yet.
neekfenwick:  any answer to the question "Why" in no way changes the truth of how to accomplish a purpose.  The "why" is the business of only the one doing the business.  We get to solve puzzles, without caring "why".

but, going backwards to dojo 1.4.2 or 1.5.2   from 1.9x   is a learning curve all by itself.  Heck , DOJO is a crucible learning curve by itself.  The documentation is joke written by short-bus grammarians with a special-needs grasp of syntax and vocabulary.

No wonder DOJO has a fraction of a percent of market share.

btw:  The answer to any "why" question is ALWAYS an excuse and it is usually women who ask why-questions ,  sooo... lets not do that.