将textarea innerhtml附加到数组,然后在数组中搜索id

时间:2013-08-25 20:05:08

标签: javascript html arrays

我正在创建一个函数,它将在数组中获得textarea的innerHTML,然后它将搜索该数组中的id。例如innerHTML是Football,Basketball,然后将存储在数组中然后ids将提取足球篮球从它,然后ids与名称足球和篮球将有backgroundColor更改onpage load.Here集群是textarea名称。

以下是代码:

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]) {
        this.style.backgroundColor="#2BBBF3";
        }
    }
}
}
function checkids() {
var check=document.getElementById("cluster").innerHTML;
selected.push(check);
//extract ids from select and change their background color
}

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');
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;
}

谢谢。

1 个答案:

答案 0 :(得分:0)

不确定这是否是正确的答案,但...... textarea没有innerHTML ...它只有一个值属性...这有点明显,因为textareas只能包含文本......

编辑:显然它确实有innerHTML,但它是只读的,只保留原始的textarea值。 See for yourself.