javascript jquery getelementbyid

javascript - document.getElementByID no es una función



document.getelementbyid(...) is null (4)

Estoy aprendiendo jQuery y estaba siguiendo un tutorial, un error muy extraño me ha dejado perplejo. Aquí está mi html:

<!doctype html> <html> <head> <title> Simple Task List </title> <script src="jquery.js"></script> <script src="task-list.js"></script> </head> <body> <ul id="tasks"> </ul> <input type="text" id="task-text" /> <input type="submit" id="add-task" /> </body> </html>

y la jQuery:

$(document).ready(function(){ //To add a task when the user hits the return key $(''#task-text'').keydown(function(evt){ if(evt.keyCode == 13) { add_task(this, evt); } }); //To add a task when the user clicks on the submit button $("#add-task").click(function(evt){ add_task(document.getElementByID("task-text"),evt); }); }); function add_task(textBox, evt){ evt.preventDefault(); var taskText = textBox.value; $("<li>").text(taskText).appendTo("#tasks"); textBox.value = ""; };

Cuando agrego elementos Al presionar la tecla de retorno, no hay problema. Pero cuando hago clic en el botón Enviar, firebug muestra este error.

document.getElementByID is not a function [Break On This Error] add_task(document.getElementByID("task-text"),evt); task-list.js (line 11)

Traté de usar jQuery en lugar de reemplazarlo con

$("#task-text")

Esta vez el error es:

$("<li>").text(taskText).appendTo is not a function [Break On This Error] $("<li>").text(taskText).appendTo("#tasks"); task-list.js (line 18 which results in the following error

He estado intentando depurar esto por algún tiempo pero simplemente no lo entiendo. Es probablemente un error realmente tonto que he cometido. Cualquier ayuda es muy apreciada.

Edición: estoy usando jQuery 1.6.1


Es document.getElementById() y no document.getElementByID() . Revise la carcasa para la Id .


Es getElementById()

Tenga en cuenta la d minúscula en Id .


Hay varias cosas incorrectas con esto como puede ver en las otras publicaciones, pero la razón por la que está recibiendo ese error es porque usted nombra su formulario getElementById. Entonces document.getElementById ahora apunta a su formulario en lugar del método predeterminado que proporciona javascript. Vea mi violín para una demostración de trabajo https://jsfiddle.net/jemartin80/nhjehwqk/ .

function checkValues() { var isFormValid, form_fname; isFormValid = true; form_fname = document.getElementById("fname"); if (form_fname.value === "") { isFormValid = false; } isFormValid || alert("I am indicating that there is something wrong with your input.") return isFormValid; }


He modificado tu script para trabajar con jQuery, si así lo deseas.

$(document).ready(function(){ //To add a task when the user hits the return key $(''#task-text'').keydown(function(evt){ if(evt.keyCode == 13) { add_task($(this), evt); } }); //To add a task when the user clicks on the submit button $("#add-task").click(function(evt){ add_task($("#task-text"),evt); }); }); function add_task(textBox, evt){ evt.preventDefault(); var taskText = textBox.val(); $("<li />").text(taskText).appendTo("#tasks"); textBox.val(""); };