javascript variable implicitly declared - javascript

so im having an issue with some javascript and html.
in my html i have a button set up with an id
<td><button id = "accept">Accept</button></td>
and in the javascript onload function i have
acceptButton = document.getElementById("accept");
But for some reason it just started to say, variable implicitly declared , and when i try add anymore javascript, the button does not function. I am very new to javascript and really struggling to work out what this issue is due to, can someone maybe shed some light on it? thanks
I tried adding var, it takes away the error but stops the buttons functionality

Use the var keyword to declare your variable:
var acceptButton = document.getElementById("accept");

It was because I loaded the javascript file at the top of the HTML before the DOM loads

Related

Javascript On Click Syntax Error?

I have a line of javascript which calls a function called "divClicked". But the targeted div is dynamically generated, so I think I need to use an on click function instead. What is wrong with my syntax?
Original line which doesn't work due to dynamically generated content.
$(".js-contentEditable").click(divClicked);
New Line with syntax error
$("#generator").on("click", ".js-contentEditable", function(divClicked);
Well, it certainly is a syntax error, but you're close:
$("#generator").on("click", ".js-contentEditable", divClicked);
When you've already got a defined function and you can reference it by name, then you can pass it to another function just with the name. You only need the function keyword when you're defining a new function. (It occurs to me that when I say, "a new function", I mean a function that didn't exist before. I do not mean a function intended to be used with new; that keyword has nothing to do with the problem at hand here. Don't be confused please :)

VBA IE call a javascript containing 'this' keyword

I am attempting to call a javascript function on a webpage that contains the 'this' keyword which is referring to the <input> textbox on the webpage. The function looks like this:
functiondostuff('hdnAttribute',this,'Key')
Using
js = "functiondostuff('hdnAttribute',this,'Key')"
Call IE.Document.parentWindow.execScript(js)
doesn't throw an error but does not produce the results of the function since this cannot be identified.
Stepping through the website this = [object DispHTMLInputElement] instead of the element name while the function is running. Anyone have any ideas?
Good Morning,
Adding more to this issue. There seems to be two problems, 1st is setting the window.event, functiondostuff begins with: if (window.event && window.event.keyCode == 13), when the function is called it exits out immediately due to the event being null. Is there a way to pass the event as 13 to the website? The second issue is submitting the "this" HTMLInputObject.
Does anyone know a method to fire the 'onkeypress' event? I am at the point of trying sendkeys to avoid calling the function but have not been able to get them to work with IE. Thanks for any suggestions!
Key point is context. If you have this HTML
<input onclick="functiondostuff('hdnAttribute',this,'Key')">
then the browser can infer context from the user interaction and set this for you correctly.
From within VBA that's a slightly different matter and you have to define context manually.
How about this:
Dim js As Variant
js = Array( _
"var input = document.getElementById('yourElementsId');", _
"functiondostuff('hdnAttribute',input,'Key');" _
)
Call IE.Document.parentWindow.execScript(Join(js, vbNewLine))
This way you get to define context yourself.
document.getElementById was just for the sake of the example. If your element has no ID, use any other method (like DOM traversal, document.querySelectorAll, document.getElementsByTagName + a loop, ...) to get a reference to the desired element.

appendChild of objects containing javascript attributes

i have this Javascript function
function webit(thumb){
webi = document.createElement("img");
webi.alt=thumb.id.replace("t", "");
webi.id = "w"+webi.alt;
webi.className = "web";
webi.src= thumb.src.replace("thm","web");
webi.height=233;
webi.onclick='alert()';
document.body.appendChild(webi);
}
which is supposed to embed a larger version of a thumbnail image the end of the document. It works fine except that any javascript function ( ie onXXX) stays resolutely null. This seems to be no matter which JS function i use and afaict any thing i try to set it to.
The above example uses
webi.onclick='alert()';
which fails leaving onclick null, though all the other statements succeed.
When in javascript the .onclick property expects a function not a string
webi.onclick=function(){ alert(); };
You could also use the addEventListener method to set an event handler
webi.addEventListener("click",function(){ alert(); });
There are two problems, you are giving quotes to alert & onclick requires a function that can be called after clicking on it, you should not call the assigned function.
webi.onclick=function(){alert()};
You might consider declaring webi as var. Without "var", you implicitly defined window.webi, and it will introduce memory leaks.
var webi = document.createElement("img");
In addition, jQuery is usually a better choice than raw js/browser API.

Assignement of a variable inside a concatenation

I have a global variable var num_tab=1;, a function that creates a link a href :
function Addsomething()
{
$("#tout").html("<a style=\""+"margin-left:-20px;"+"\" onClick=\"eval(num_tab=2)\" href=\""+"#tab1"+"\" data-toggle=\""+"tab"+"\">SELECT</a>");
Bla,Bla..
$("#champ1").append('<li id=\"1\" class="champ" onclick="insertAtCaret("sousTab'+num_tab+'");" value=\"1\">1</li>');
}
What i want to do is to create a href that when clicked changes the value of the variable num_tab, but if you can see the href is inside a jquery html(), which makes me confused about how to assign a value to the variable. I almost tried everything: onClick=\"num_tab=2\",onClick=\""+num_tab+"=2\"
Actually i tried something: when i write onclick='num_tab=2;alert("+num_tab+");' i still get the initial value of num_tab, seems it's more like a problem of local and global variable and i can't figure out it yet.
Please don't use eval(). It's insecure and not the appropriate tool for this job. Just assign a function to the onclick:
$("#tout").html("<a onclick='set_num_tab(2)'">); //fill out the rest of this line
function set_num_tab(value) {
num_tab = value;
}
That should give you an idea of how to do it. btw there's no reason you can't use single quotes around an onclick like that.
Alternately, this would work:
$("#tout").html("<a onclick='num_tab=2'">);
But that's pretty messy. I try to avoid inline JavaScript.

AJAX IE doesn't find element ( getElementByID() )

I've searched everywhere i could but i did not find a solution to this specific problem.
To put it simple. I have a page with some javascript code and a DIV. I use ajax to load a second page inside that DIV. The loaded page has one element which id is "someid".
someid is <input type="hidden" id="someid" name="someid" value="sdasasdadad" />
then, on the loaded page i call a function that is defined on the "global" page that begins with these two lines:
var=document.getElementById("someid").value;
alert(var);
works fine on firefox but not on IE. On IE it doesnt display the alert and doesnt execute any code after those lines.
I think the problem is that when the javascript code was evaluated, the "someid" element didnt exist yet and IE doesnt seem to understand that now it exists after i loaded a page using ajax. I hope i made myself clear?
I need to do things this way because this is only a small part of a bigger interface. I have no knowledge on jquery but it should work with this javascript code too i guess! How can i make this work?
Thanks a lot in advance!
var is used to declare variables. Do something like var el = document.getEl...
This is assuming your code is exactly the same as your production code, otherwise please show us the real code
EDIT: Since you said it "works" in Firefox, your code is probably inconsistent, since var = 3 in Chrome throws a SyntaxError and I imagine it would do so in Firefox too.
The var word is a keyword in Javascript. Here you are trying to use a keyword as an identifier which is illegal. Using a name other than var like myVar will fix the problem.
var myVar =document.getElementById("someid").value;
alert(myVar);
Try it like that. var is a reserved keyword for javascript
var someVar = document.getElementById("someid").value;
alert(someVar);
I would try debugging this by alerting the element and then the value :
var element = document.getElementById("someid");
alert(element);
var valueOfElement = element.value;
alert(valueOfElement);
I'm sure you haven't but you can't use var as a variable name, it's a Javascript keyword.

Categories