Skip to content Skip to sidebar Skip to footer

Html Input - Getting The Value Via Innerhtml Or Xmlserializer

I have an input box on a html page. I know I can get just the value, but I want the entire input string, i.e. , but with the value present: Copy
<inputid="myInput"value="my entry that I just typed in"/>
And if you want to add / at the end:

myVar = document.getElementById("myInput").outerHTML;
if(myVar.charAt(myVar.length - 1) !== "/"){
console.log(myVar.slice(0, myVar.length-1) + "/>");
}
<inputid="myInput"value="my entry that I just typed in"/>

Solution 2:

I ended up doing the following: Serializing with XMLSerializer, which solved the / problem. And I just got the values from the DOM and inserted them myself.

function htmlCleanup(htmlDiv) {

    //serialize the html.  It will lack the latest user changes in the inputs.vars=newXMLSerializer();
    varstr= s.serializeToString(htmlDiv);
    varlines= str.split("\n");
    //get all of the inputs in the divvarinputs= htmlDiv.getElementsByTagName('input');

    //Here we put in the latest valuesvarinputIndex=0;
    for (vari=0; i < lines.length; i++) {
        varline= lines[i].trim();
        if (line.indexOf('<input') >= 0) {
            varvalue= inputs[inputIndex].value;
            lines[i] = fixInputValue(line, value);
            inputIndex++;
        }
    }

    str = lines.join('\n');
    //Some other weird aftertaste stuff that needs fixing. <tbody> is added to tables - wrongly.//the div at the top is also a problem.//Then we turn it all into proper xhtml for the trip back to the server.varcontents= str.replace('<div xmlns="http://www.w3.org/1999/xhtml" id="documentEditBody">', '');
    contents = contents.replace(/<tbody>/g, '');
    contents = contents.replace(/<\/tbody>/g, '');
    contents = '<?xml version="1.0" encoding="UTF-8"?><html><head></head><body><div>' + contents + '</body></html>';

    return contents;
}

function fixInputValue(input, value) {
    varvaluePos= input.indexOf('value');
    varresult="";
    if (valuePos > -1) {
        for (vari= valuePos + 7; i < input.length; i++) {
            varchr= input[i];
            if (chr === '"') {
                varlast= input.substring(i + 1, input.length)
                result = input.substring(0, valuePos - 1) + ' value="' + value + '" ' + last;
                break;
            }
        }
    }
    return result;
}

Post a Comment for "Html Input - Getting The Value Via Innerhtml Or Xmlserializer"