'Object is not a function' - onclick event - javascript

Before I start, no I have no issues that I can find with semicolons, and I'm passing NO values to the function.
When I try to do function "login()" from the console, it works just fine, but when I click an HTML input button to call it, I get "Uncaught TypeError: object is not a function". This is in Chrome by the way.
var aj=new XMLHttpRequest();
var loginf=document.forms["login"];
var regf=document.forms["reg"];
function login(){
var errors=document.getElementById("login_err");
var errs=[];
var uname=loginf['uname'];
var pass=loginf['pass'];
var unameVal=uname.value;
var passVal=pass.value;
if(unameVal.length<4 && unameVal.length>0){
errs.push("Username too short. Try again please.");
}
else if(unameVal.length>18){
errs.push(innerHTML="Username is too long. Try again please.");
}
else if(unameVal.length==0){
errs.push("Please enter a username.");
}
if(passVal.length<8){
errs.push("Password too short. Try again, please.");
}
else if(passVal.length>30){
errs.push("Password is too long. Try again please.");
}
if(errs.length==0){
aj.onreadystatechange=function(){
if(aj.readyState>=4 && aj.status==200){
if(aj.responseText=="suc"){
window.location="/new.php";
}
}
}
aj.open("POST","/inc/php/core.php",false);
aj.setRequestHeader("Content-type","application/x-www-form-urlencoded");
aj.send("login=true&data="+data);
}
else{
errors.innerHTML="<ul>";
for(var x=0;x<errs.length;x++){
errors.innerHTML+="<li>"+errs[x]+"</li>";
}
errors.innerHTML+="</ul>";
}
}
function reg(){
}
And then the input button..
<input type="button" class="submit" value="Log in" style="width:25%" onclick="login();"/>
I see nothing wrong with the code.
Thank you in advance if you can find Waldo in this code.
jsFiddle HERE: http://jsfiddle.net/6sa7M/2

It turns out that the function name "login" was both a reference to the form and function.
I changed "login()" to "loginUser()", and typed "login" into the console, and the form was returned, so they were indeed conflicting with each other.
Thank you again for all the help and special thanks to Marc for the true answer.

Related

window.prompt stop working when adding function

var user = window.prompt("Welcome to the Vacation Planner. Please enter yourname");
document.getElementById("greeting").innerHTML += ", " + user;
var enterDays = document.getElementById("enterDays");
/*
function.calculateDays(){
var dayMessage = document.getElementById("dayMessage");
if(enterDays <4){
dayMessage.innerHTML = "Short trips are always worth it!";
}
else if(enterDays<7){
dayMessage.innerHTML = "Cool, you'll be there for a week"
}
else{
dayMessage.innerHTML = "You'll have plenty of time to relax and have fun!"
}
}*/
enterDays.onclick = calculateDays;
When I enter this code, the window.prompt work correctly. But when I uncomment the function.calculateDays, the window.prompt stop working. Can anybody explain why is it happening and how to solve it?
Thanks a lot!!!
function.calculateDays is not valid syntax and will throw a syntax error that will prevent future code from calling, to fix it replace it with function calculateDays.

javascript if condition is not working

i'm trying to work the following code of script. but its not working. i don't know what is the problem. the x variable i created contains nothing. but still the if condition is not working. i have tried printing the x variable in an alert box and it prints nothing, which means that it contains nothing. But in its not picking up the condition don't know why. And there are no console errors.
<div id="test">
</div>
<script>
var x = document.getElementById("test").innerHTML;
if(x == '') {
document.getElementById("test").innerHTML = 'There are No friends posts yet my love!!';
}
</script>
Your variable x contains not empty string but some spaces. Try to use trim() function to remove these symbols:
if(x.trim() == ''){
...
}
This will work!
<script>
var x=document.getElementById("test").innerHTML;
if(x.trim() == ''){
document.getElementById("test").innerHTML='There are No friends posts yet my love!!';
}
</script>
use innerText
if(x.innerText === ''){
https://jsfiddle.net/s4aLcfm5/
The issue is that your 'x' variable is not empty. It contains two return characters. Try removing all the white-space between your open and close div statement. Here is the corrected code:
<div id="test"></div>
<script>
var x=document.getElementById("test").innerHTML;
debugger;
if(x == ''){
document.getElementById("test").innerHTML='There are No friends posts yet my love!!';
}
</script>`
Simply add or remove returns within the div statement to see this work or not work.
Your code works. It all comes down to the way that your html is written:
<div id="test">
// This is an unnecessary space that's what is making your condition fail
</div>
<script>
var x=document.getElementById("test").innerHTML;
if(x == ''){
document.getElementById("test").innerHTML='There are No friends posts yet my
love!!';
}
</script>
Clean up your html: <div id="test"></div>
And you don't need anything else

Syntax error on javascript

I am getting syntax error
Uncaught SyntaxError: missing ) after argument list
My code
<button onclick="alert('document.getElementById('todo.age').value');">onclick</button>
in controller add
$scope.alert = function(age) {
alert(age);
}
in html change like this:
<button ng-click="alert(todo.age);">onclick</button>
Try this:
<button onclick="alert(document.getElementById('todo.age').value);">onclick</button>
You shouldn't quote the argument to alert if you want to call the function.
It will start working better if you separate your HTML code and JavaScript.
Kind of this.
HTML:
<button id="button">onclick</button>
JavaScript:
window.onload = function()
{
var button = document.getElementById('button');
button.onclick = function()
{
//do some action here
}
}
This will help you to avoid simple syntax errors.
if u need value of the todo.age id values use the following code. <button onclick="alert(document.getElementById('todo.age').value);">onclick</button>
or else of u want to print the whole text document.getElementById('todo.age').value like this
use
<button onclick="alert('document.getElementById('todo.age').value');">onclick</button> code/

AJax, How to get the value from a node in an XML file

I want to get the stats from my XML when someone is asking for a particular name, but my javascript doesn't work like I want.>br>
Here's my XML :
<player>
<forward><name>Joe</name><stats>45</stats></forward>
<forward><name>Jack</name><stats>42</stats></forward>
<forward><name>Peter</name><stats>34</stats></forward>
<forward><name>Steve</name><stats>21</stats></forward>
<goalie><name>Pat</name><stats>2.34</stats></goalie>
</player>
Here's my HTML (ajax) :
<html>
<head>
<script language="JavaScript">
function LoadDoc(vValue) {
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "player.xml",true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
Answer(xmlhttp.responseXML, vValue);
}
}
xmlhttp.send(null);
}
function Answer(doc, ParamValue) {
var counts=doc.getElementsByTagName("forward");
for(var i=0;i < counts.length; i++){
alert(counts.length)
var vname = counts[i].getElementsByTagName('name');
alert(vname[i].firstChild.nodeValue)
var vstats = counts[i].getElementsByTagName('stats');
alert(vstats[i].firstChild.nodeValue);
if (vname[i].firstChild.nodeValue == ParamValue)
{
alert(stats[i].firstChild.nodeValue);
}
}
}
</script>
</head>
<body>
<form>
<input type="field" id="champ" />
<input type="button"
onclick="javascript:LoadDoc(document.getElementById('champ').value);" />
</form>
</body>
</html>
The first ALERT is good finding 4 elements
the second ALERT works giving me JOE.
The third ALERT works giving me 45 (the number) of the stats of the first forward
But strangely, the alert are launch only once... I don't have 4 times the Alert, why it doesn''t turn 4 times inside the loop ?
Any Idea of what's wrong ?
Or a better solution to find the stats ?
Look at the console for any error messages. At a glance, the line alert(stats[i].firstChild.nodeValue); references an undeclared variable stats, so if that line executes, the script should throw a ReferenceError and terminate, which would cause the alerts to fire only once.
Thanks Dagg Nabbit, finally I found it. with the error found previously, I suppose that the vname[i] doesn't exist, so I replace the "i" with "0" so it's now vname[0]. For sure I'm looking the first element named "name" and not the second.I replace the vstats[i] with vstat[0] and it works perfectly.

how do i find what's going wrong with my code

I have this function for my sql javascript
PATH="http://localhost/dhodia/";
try{
var tags=url.replace(PATH,"");
var spl=tags.split("/");
if(spl[0]=="i"){
if(spl.length>=2 && spl[1]=="hdd"){
}
addtoopen($("#"+spl[1]+"opn").attr("jso"));
menu_action(spl[1],url)
}
}catch(err){
var txt="There was an error on this page.\n\n";
txt+="Error description: " + err.message + "\n\n";
txt+="Click OK to continue.\n\n";
alert(txt);
}
i found there is problem at replace statement
if i use url as "http://localhost/dhodia/i/hdd" it will do replace
but if i use "http://localhost/dhodia/i/hdd/tarun" it is not doing replace
i want to track why it is not taking replace at this line
EDIT
I am using this in function as shown below
function hstppst(url){
config.pre=url;
if(url==PATH || url==PATH+"i/dashboard"){
$E("#"+current.open).hide();
unfixbox()
$E("#dashboard").show();
addtoopen(dsjsn);
document.title="My DashBoard";
current.open="dashboard";
}else{
var tags=url.replace(PATH,"");
var spl=tags.split("/");
if(spl[0]=="i"){
if(spl.length>=2 && spl[1]=="hdd"){
}
addtoopen($("#"+spl[1]+"opn").attr("jso"));
menu_action(spl[1],url)
}
}
}
and this function call when window.onpopstate changes
in onpopstate i pass url to this function
problem is that code is not executing after var tags=url.replace(PATH,""); and because i am using this function in onpopstate function if any error occurs poage refreshed and i am track what is error i am getting.
Please take a look at the usage of replace method of the string object:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace
You mistakenly use replace.

Categories