Your main error is that you are trying to call a function by assigning it to the button as a id
. You should use the attribute onclick
failing.
On the other hand, the body of your function should be with the form:
function nombreFuncion(){
//Código
}
Once you have made the function, you can refer to the input
using the function document.getElementById
(Javascript method) or $("#id")
(JQuery method) and obtain its value by referring to the attribute value
or the function val()
respectively.
Finally, assign that new value to your list.
Your modified example:
Using Javascript
let arrayPaises= ['eeuu', 'colombia', 'noruega', 'islandia', 'peru']
arrayPaises.map((e, key)=>{
$('ul').append('<li>'+e+'</li>');
});
function insertar(){
var contenido = document.getElementById("contenido").value;
$('ul').append('<li>'+contenido+'</li>');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="pincipal"></ul>
<input type="text" id="contenido">
<button onclick="insertar()">insertar</button>
Using JQuery
let arrayPaises= ['eeuu', 'colombia', 'noruega', 'islandia', 'peru']
arrayPaises.map((e, key)=>{
$('ul').append('<li>'+e+'</li>');
});
function insertar(){
var contenido = $("#contenido").val();
$('ul').append('<li>'+contenido+'</li>');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="pincipal"></ul>
<input type="text" id="contenido">
<button onclick="insertar()">insertar</button>