Necesito escribir una ID (del 1 al 10) y que se muestre en una tabla, pero no sé cómo llevar la ID que escribí a la impresión, ¿cómo visualizo SOLO los datos de cierta ID del archivo json? ?
<label for="ide">ID:</label> <input id="ide" type="search" placeholder="ID" name="ide"> <button id="boton" type="submit">Buscar</button> <table class="table bordered"> <thead> <tr> <th>UserID</th> <th>ID</th> <th>Title</th> <th>Completed</th> </tr> </thead> <tbody id="tabla"> </tbody> </table> function ajax() { const url = "https://jsonplaceholder.typicode.com/users"; axios.get(url) .then((res) => { show(res.data) }) .catch((err) => { console.log(data); }) } function show(data) { for (const id of data) { tabla.innerHTML += ` <tr> <td>${id.id}</td> <td>${id.name}</td> <td>${id.username}</td> <td>${id.email}</td> </tr> ` } } document.getElementById("boton").addEventListener("click", function() { ajax(); });Declarar una variable para id primero
let id = 0 document.getElementById("ide").addEventListener("onchange", function(e) { id = e.target.value });Su función ajax debería ser así:
function ajax() { const url = `https://jsonplaceholder.typicode.com/users/:${id}`; axios.get(url) .then((res) => { show(res.data) }) .catch((err) => { console.log(data); }) }