function to extract ids and find and change background color
Im making a function that will get innerHTML of textarea in an array and
then it will search ids in that array.For example innerHTML was
Football,Basketball, then that will be stored in an array and then ids
Football Basketball will be extracted from it and then ids with the name
football and basketball will have backgroundColor changed onpage load.Here
cluster is the textarea name.
window.onload = checkids;
window.onload = btnsInit;
var selected = {};//keeps list of selected links
var val="";
function checkids() {
var check=document.getElementById("cluster").value;
selected = check.split(',');
var j, i, a = document.getElementById('boxpopup').getElementsByTagName('a');
for(j=0;j < selected.length; j++) {
for(i=0;i < a.length ; ++i) {
if(selected[j]==a[i]) {
var x=a[i];
x.style.backgroundColor="#2BBBF3";
}
}
}
}
function btnsInit() {
var i, a = document.getElementById('boxpopup').getElementsByTagName('a');
for (i = 0; i < a.length; ++i) {
a[i].onclick = btnClick;
}
}
function btnClick(e) {
if (selected[this.id]) {
this.style.backgroundColor ="grey";
delete selected[this.id];//deleting if already been clicked
} else {
this.style.backgroundColor ="#2BBBF3";
selected[this.id] = this.id;//adding to the selected list
}
updateTextArea();
xPreventDefault(e);
return false;
}
function updateTextArea() {
var cluster = document.getElementById('cluster');
var val="";
for ( var id in selected) {
val += selected[id] + ",";
}
cluster.value = val;//updating from selected list
}
function xPreventDefault(e) {
if (e && e.preventDefault)
e.preventDefault();
else if (window.event)
window.event.returnValue = false;
}
No comments:
Post a Comment