I have a form with a select dropdown, and my select tag looks like this:
<select name='preset' onchange='preset(this);'>
Right now I have my JavaScript function just do alert('test');. Well, when I change my selection in the dropdown, I'm getting an error saying "preset is not a function". Yes, I verified that it's spelled right, and I even did a generic call to it on page load and got my alert.
If I change my function name to something else, like presetx it works just fine. So I thought maybe "preset" was some kind of reserved word in JavaScript, but I can't seem to find anything saying as such. Why would this happen?
Update
Currently I don't have anything else on my test page except for my form and the function. No framework includes or other code, so I know it's not anything like that.
Some browsers map elements with name attributes to global variables. So <select name='preset' onchange='preset(this);'> actually creates (in some browsers) a global property preset. This overwrites the preset function.
Since preset is now an HTMLSelectElement object, not a function, you get a "not a function" error.
Related
I'm trying to use jQuery and AJAX to validate that users entered a number in a particular field and that they didn't leave it blank and I'm a little confused as to why I can seem to do one, but not the other.
I'm doing this in a jQuery change() function so any time they change the value in that field, it updates it in the database without refreshing the whole page and it works fine until I try to use isNull() to validate.
I'm saving their input to a variable called UserInput and first checking to make sure it's a number with this:
if (!isNaN(UserInput))
which works perfectly. I'm also trying to check and make sure it isn't empty by using this:
if (isNull(UserInput))
Intellisense completes isNull() for me just like it did for isNaN() and all appears well in Visual Studio, it compiles without error. I've also tried isNullOrUndefined() here with a similar result, intellisense completes it for me and all seems well. Right up until I change the value in the field, at which point it promptly gives me this error:
JavaScript runtime error: 'isNull' is undefined.
I'm not sure why it's undefined (especially since intellisense is completing it for me) or how to go about defining it.
I also tried this because it seemed like it covered all the bases, not just isNull():
https://stackoverflow.com/a/5515349/8767826
and I put an alert() inside the if and I didn't get an error, but my alert didn't fire either.
The end goal is to get it to change to a zero on the client side if they leave do leave it blank.
Anyway I'm kind of stumped and I appreciate any help anyone can offer.
Thanks
There's no need for an isNull function; you can check
if (UserInput === null)
isNaN exists because NaN, unlike every other value in JavaScript, is not equal to itself.
But null doesn't mean the field is blank! If the field is blank, its value will be the empty string. Check for that instead:
if (UserInput === '')
I am still new a Javascript tried earching and tried the development tool in Chrome, too see if I could find the problem.
Working in Intellij IDEA 13, Java, javascript and xhtml.
My problem is that I have a piece of javascript, then in IDEA when moused over, says that
Expression Expected
the javascript code looks the following
<script type="text/javascript>
function nameOfFunction(){
if(#{trendAnalysisLocationReportController.model.showTargetLine}){
this.cfg.series[this.cfg.data.length-1].pointLabels = {
show: false
};
}
}
<\script>
the method in the if sentence is a java method with a boolean return value.
the error is shown when hovering
'#{'
if Had a look at the following questions, before :
Expected Expression
boolean in an if statement
But didnt get me a solution.
what Iam I doing wrong ?
It looks as though the problem is the part that you've got within the #{...} block. Without knowing the context, it's hard to be sure, but is this something in a view/JSP page that's supposed to be replaced with a property at runtime? The if block will expect the part inside the brackets to be a boolean value, so if that's rendered to either 'true' or 'false' it would execute at runtime but would likely show the error you're seeing in your IDE as it's not actually a valid piece of JavaScript. If, on the other hand, you're expecting to be able to call your Java method/property from your JavaScript code, you're going to need to do something that requests that value from the server-side code - AJAX or similar.
Also worth noting that we can't see what this.cfg is supposed to represent. If that's your entire script block, then there's nothing that defines the cfg object within the current scope.
One last thing, you should change the <\script> end element to as it won't be understood properly by some browsers.
My JavaScript code is:
var headline = document.getElementById("mainHeading");
headline.onClick = function() {
headline.innerHTML = "You clicked the headline.";
};
But nothing happens when I click the headline. It just works when I use onclick instead of onClick (without capital letter C).
The thing is when I debug with both Chrome DevTools and Firebug, they don't show onClick as a syntax error.
Could anyone tell me how to debug it?
The event is onclick (all lowercase). JavaScript is case sensitive so you must match the casing. When you set onClick you are setting a custom property on your element to the function you specified. The element stores the value, but nothing is ever accessing that property. It's a feature of JavaScript that you can set arbitrary properties on any object so there's nothing in the debuggers that will let you catch this unfortunately.
They won't show onClick as syntax error, because it's not.
What you're doing is setting a variable named onClick on the object headline as a function. Everything is fine with that. No error here.
It won't be called though, because the 'browser' will call onclick when a click occured.
And if you don't call onClick yourself, it will never be.
Use onclick.
I'm having a strange issue in IE8 where I'm trying to grab something by simply doing:
window.frames.frames[0].name; // get the name of the inner iFrame object
Nothing fancy, but when script is ran, IE7-8 interpret it like this:
window.frames.frames.0.name;
// which in-turn gives the error
// 'window.frames.frames.0.name' is null or not an object (which is not true)
Why and how is it converting this, and why isn't it even working anymore??
If I type the first one window.frames.frames[0].name; into the console of IE8, it grabs the correct iFrame. But typing in what IE8 interprets (window.frames.frames.0.name;), doesn't work at all... (strangely says, "Expected ';'", which makes zero sense haha.
Anyone ever run into an issue like this?
That dot notation in the error message is just a string the browser uses, poor choice on the browser developers.
The line `window.frames.frames[0].name` does not make sense.
I would expect
window.frames[0].name
or if it is nested frame in a frame
window.frames[0].frames[0].name
window.frames is an array, is it not? Shouldn't you be indexing the first frame?
window.frames[0].frames[0].name;
Does it work if you put parentheses around the the call? like this:
(window.frames.frames[0]).name; // get the name of the inner iFrame object
Also do you really mean do reference window.frames.frames[0] and not just window.frames[0]?
Or do you mean:
window.frames[0].frames[0].name; // get the name of the inner iFrame object
I'm going berserk with this. Although http://www.prototypejs.org/api/element/insert is far from being the best documentation page ever, I struggle with a really stupid simple implementation:
$('account').insert({'top':new Element('a')});
I also tried with a plain HTML string instead of new Element(a), but it doesn't change anything... Can you spot what's wrong with what I'm doing ?
Prototype returns null from $("foo") if no element with "id" value "foo" is on the page. If you're using the "id" value "account" on multiple elements, anything might happen, so don't do that. Otherwise make sure there's an element with "id" value "account" on the page when that code runs.
In JavaScript, the semicolon terminates a statement. You don't want to terminate the statement there, you wanted to call .insert on the result of $('account'), so don't put a semicolon there.
According to the documentation you linked, you're also missing a set of curly braces and some quotes.
$('account').insert({'top': new Element('a')});