简体   繁体   中英

Javascript function not recognizing id in getElementById

I am adding a row to a table, and attached an ondblclick event to the cells. The function addrow is working fine, and the dblclick is taking me to seltogg , with the correct parameters. However, the var selbutton = document.getElementById in seltogg is returning a null. When I call seltogg with a dblclick on the original table in the document, it runs fine. All the parameters " selna " have alphabetic values, with no spaces, special characters, etc. Can someone tell me why seltogg is unable to correctly perform the document.getElementById when I pass the id from addrow ; also how to fix the problem.

function addrow(jtop, sel4list, ron4list) {
    var tablex = document.getElementById('thetable');
    var initcount = document.getElementById('numrows').value;
    var sel4arr = sel4list.split(",");
    var idcount = parseInt(initcount) + 1;
    var rowx = tablex.insertRow(1);
    var jtop1 = jtop - 1;

    for (j = 0; j <= jtop1; j++) {
        var cellx = rowx.insertCell(j);
        cellx.style.border = "1px solid blue";

        var inputx = document.createElement("input");

        inputx.type = "text";
        inputx.ondblclick = (function() {
            var curj = j;
            var selna = sel4arr[curj + 2];
            var cellj = parseInt(curj) + 3;
            inputx.id = "cell_" + idcount + "_" + cellj;
            var b = "cell_" + idcount + "_" + cellj;
            return function() {
                seltogg(selna, b);
            }
        })();

        cellx.appendChild(inputx);
    } //end j loop
    var rowCount = tablex.rows.length;
    document.getElementById('numrows').value = rowCount - 1; //dont count header
} //end function addrow

function seltogg(selna, cellid) {
    if (selna == "none") {
        return;
    }
    document.getElementById('x').value = cellid; //setting up for the next function
    var selbutton = document.getElementById(selna); //*****this is returning null
    if (selbutton.style.display != 'none') { //if it's on
        selbutton.style.display = 'none';
    } //turn it off               
    else { //if it's off
        selbutton.style.display = '';
    } //turn it on  
} //end of function seltogg

You try, writing this sentence: document.getElementById("numrows").value on document.getElementById('numrows').value

This is my part the my code:

contapara=(parseInt(contapara)+1);

                document.getElementById("sorpara").innerHTML+="<li id=\"inputp"+contapara+"_id\" class=\"ui-state-default\"><span class=\"ui-icon ui-icon-arrowthick-2-n-s\"></span>"+$('#inputp'+contapara+'_id').val()+"</li>";   

Look you have to use this " y not '. TRY!!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM