Skip to content

Commit 99bb555

Browse files
committed
Merge pull request #1024 from DarkPrince304/createRadio
Implemented the createRadio() functionality issue #952
2 parents a8f9bb0 + 6dd5923 commit 99bb555

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

lib/addons/p5.dom.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,91 @@
525525
return self;
526526
};
527527

528+
/**
529+
* Creates a radio button <input></input> element in the DOM.
530+
*
531+
* @method createRadio
532+
* @param {String} [divId] the id and name of the created div and input field respectively
533+
* @return {Object/p5.Element} pointer to p5.Element holding created node
534+
*/
535+
p5.prototype.createRadio = function() {
536+
var radios = document.querySelectorAll("input[type=radio]");
537+
var count = 0;
538+
if(radios.length > 1){
539+
console.log(radios,radios[0].name);
540+
var length = radios.length;
541+
var prev=radios[0].name;
542+
var current = radios[1].name;
543+
count=1;
544+
for(var i = 1; i < length; i++ ){
545+
current = radios[i].name;
546+
if(prev != current){
547+
count++;
548+
}
549+
prev = current;
550+
}
551+
}
552+
else if (radios.length == 1){
553+
count = 1;
554+
}
555+
var elt = document.createElement('div');
556+
var self = addElement(elt, this);
557+
var times = -1;
558+
self.option = function(name, value){
559+
var opt = document.createElement('input');
560+
opt.type = 'radio';
561+
opt.innerHTML = name;
562+
if (arguments.length > 1)
563+
opt.value = value;
564+
else
565+
opt.value = name;
566+
opt.setAttribute('name',"defaultradio"+count);
567+
elt.appendChild(opt);
568+
if (name){
569+
times++;
570+
var ran = Math.random().toString(36).slice(2);
571+
var label = document.createElement('label');
572+
opt.setAttribute('id', "defaultradio"+count+"-"+times);
573+
label.htmlFor = "defaultradio"+count+"-"+times;
574+
label.appendChild(document.createTextNode(name));
575+
elt.appendChild(label);
576+
}
577+
return opt;
578+
};
579+
self.selected = function(){
580+
var length = this.elt.childNodes.length;
581+
if(arguments[0]) {
582+
for (var i = 0; i < length; i+=2){
583+
if(this.elt.childNodes[i].value == arguments[0])
584+
this.elt.childNodes[i].checked = true;
585+
}
586+
return this;
587+
} else {
588+
for (var i = 0; i < length; i+=2){
589+
if(this.elt.childNodes[i].checked == true)
590+
return this.elt.childNodes[i].value;
591+
}
592+
}
593+
};
594+
self.value = function(){
595+
var length = this.elt.childNodes.length;
596+
if(arguments[0]) {
597+
for (var i = 0; i < length; i+=2){
598+
if(this.elt.childNodes[i].value == arguments[0])
599+
this.elt.childNodes[i].checked = true;
600+
}
601+
return this;
602+
} else {
603+
for (var i = 0; i < length; i+=2){
604+
if(this.elt.childNodes[i].checked == true)
605+
return this.elt.childNodes[i].value;
606+
}
607+
return "";
608+
}
609+
};
610+
return self
611+
};
612+
528613
/**
529614
* Creates an &lt;input&gt;&lt;/input&gt; element in the DOM for text input.
530615
* Use .size() to set the display length of the box.

0 commit comments

Comments
 (0)