1

El código HTML y JS es el siguiente:

            window.onbeforeunload = confirmExit;
            function confirmExit(){
                return "Ha intentado salir de esta pagina. Si ha realizado algun cambio en los campos sin hacer clic en el boton Guardar, los cambios se perderan. Seguro que desea salir de esta pagina? ";
            }

            var table_inputs = 0;
            function addTextarea(id, name) {
                var table     = document.getElementById(id);
                var row       = table.insertRow(2);
                var cell      = row.insertCell(0);
                var input     = document.createElement("textarea");
                table_inputs++;
                input.rows    = "4";
                input.cols    = "170";
                input.name    = name + table_inputs;
                input.id      = id + table_inputs;
                var campo     = document.createElement("input");
                campo.type    = "button";
                campo.value   = "Borrar";
                campo.onclick = function () {
                    var fila  = this.parentNode.parentNode;
                    var tbody = table.getElementsByTagName("tbody")[0];
                    tbody.removeChild(fila);
                }
                cell.appendChild(input);
                cell.appendChild(campo);
            }

            function vaciar_campo(input1) {
                input1.value = "";
            }

            var textArea = document.getElementById('tesis');

            function addTag(){
            var current_value = textArea.value;
            var new_tag = "<a href='aqui introduzca el link'>Aqui la palabra a móstrar</a>";
            textArea.value = current_value + ' ' + new_tag;
            }
      
<form name="formulario" id="formulario" action="creador.php" method="post" width="30%">
<table id="tesisTable" border="3" width="100%">
                <tr>
                    <td><h1>TESIS DOCTORAL</h1>
                    </td>
                </tr>
                <tr>
                    <td>
                        <textarea name="tesis" id="tesis" rows="4" cols="170"></textarea>
                        <button onclick="addTextarea('tesisTable', 'tsis')" type="button" name="tesis2" id="tesis2">Añadir</button>
                        <button onclick="addTag()" type="button" name="tesis3" id="tesis3">Add tag</button>
                    </td>
                </tr>
            </table>
</form>

En el Snippet me funciona pero a la hora de probarlo en mi página aparece este error:

Uncaught TypeError: Cannot read property 'value' of null
 at addTag (VM94 datos.html:41)
 at HTMLButtonElement.onclick (datos.html:71)
1
  • Solucionado, añadiendo la var textarea dentro de addTag. Gracias por tu ayuda. el 27 mar. 2017 a las 7:55

1 respuesta 1

2

No está cogiendo la referencia en la variable textArea.

En vez de:

 var textArea = document.getElementById('tesis');
 function addTag() {
     var current_value = textArea.value;
     var new_tag = "<a href='aqui introduzca el link'>Aqui la palabra a móstrar</a>";
     textArea.value = current_value + ' ' + new_tag;
 }

Mete la inicializacion dentro:

function addTag() {
    var textArea = document.getElementById('tesis');
    var current_value = textArea.value;
    var new_tag = "<a href='aqui introduzca el link'>Aqui la palabra a móstrar</a>";
    textArea.value = current_value + ' ' + new_tag;
}

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.