0

i am adding a input file tag and a link using a javascript function, works great. Now i want to add also a radio button and a text whit this radio... i can add the radio whit no problems, but the text... idk how.

here is the code...

addCampo = function () {  
    nDiv = document.createElement('div');
    nDiv.className = 'archivo';
    nDiv.id = 'file' + (++numero);


    nCampo = document.createElement('input');
    nCampo.name = 'archivos[]';
    nCampo.type = 'file';

    a = document.createElement('a');
    a.name = nDiv.id;
    a.href = '#';
    a.onclick = elimCamp;
    a.innerHTML = ' Eliminar';

    portada = document.createElement('input');
    portada.name = 'portada';
    portada.type = 'radio';
    portada.value = '1';

    nDiv.appendChild(nCampo);
    nDiv.appendChild(portada);

    // HERE I WANT A SIMPLE TEXT SAYING WHATS DOES THE RADIO =) 

    nDiv.appendChild(a);

    container = document.getElementById('adjuntos');
    container.appendChild(nDiv);
}

this is working just fine! the only thing i dont know is how to add text whitout tags...

1 Answer 1

1

You need

text = document.createTextNode('what the radio does');
nDiv.appendChild(text);

Although it's better to use a label, because then you don't have to sharp-shoot the radio button. In that case you'd need:

portada.id = 'portada';
text = document.createElement('label');
text.innerText = 'what the radio does';
text.for = 'portada';
nDiv.appendChild(text);

Edit: as mentioned in the comments, innerText is not necessarily supported by all browsers, sorry! Just use innerHTML instead, use textContent if you don't care about old versions of IE, or create a text node and add it to the label node.

Sign up to request clarification or add additional context in comments.

1 Comment

innerText may be not recognized by all browsers, for example Firefox knows only textContent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.