HTML UI - JS variable declaration fails

HTML UI - JS variable declaration fails

by Christian Gasde -
Number of replies: 4

Hi,

since some days we are using the 'HTML-UI' question-typ and I'm wondering why I can't use

  • let 
  • const
  • var
while declaring a variable in JS.

When I start with a variablename it works (1st screenshot) ...

Varialbe declaration fails

   ... but starting with let (or const or var) nothing happens.( 2nd screenshot)Variable declaration fails


Any help is appreciated!

Best Regards from Coburg
Chris

In reply to Christian Gasde

Re: HTML UI - JS variable declaration fails

by Richard Lobb -
This isn't really a question about the HTML-UI - it's about a specific question type using that UI, written by one of the contributors to this forum some time back. I'd want to have both the question prototype and the particular question instance to understand what's going on. But in your example your let statement is assigning to a variable ElementObject not pElementObject, so certainly that won't work.

If that's not the problem please post XML exports of the question prototype you're using and the particular question itself.
In reply to Richard Lobb

Re: HTML UI - JS variable declaration fails

by Christian Gasde -
Hi Richard,

sorry - the missing 'p' (ElementObject instead of pElementObject) happend due to taking a screenshot... but this is not the problem.

// Javascript:
pElementObject = document.getElementById("bspText"); // THIS works...

pElementObject.firstChild.nodeValue = "Text des p-Elements wurde nachträglich mit JS verändert";

BUT:
let (or var or const) pElementObject = document.getElementById("bspText"); // DOESN'T work. ?!?


To tell the students not to use any let or var or const while declaring a varialbe in JS will be embarrassing.

Apart from that 'bug' the HTML-UI is great and in its simplicity exactly what we need.

I attached the XML.

Thankx again for your help!!

Best Regards from Coburg
Chris
In reply to Richard Lobb

Re: HTML UI - JS variable declaration fails

by Richard Lobb -
You'll see the problem if you open the developer tools window ( SHIFT + CTRL + I) and look at the console output. You get lots of errors like "Identifier 'pElementObject' has already been declared".

The underlying code for that question - which is in the Global Extra field - evaluates the output window on every key event. If you put let thing = 10 in the JS field, you are assigning a value to a global value (i.e. an attribute of the window object). The semantics of let and const prevent reassigning a value to an existing variable, hence your problem.

If instead you get your students to wrap their code in a function, such as:  

function myCode() {
    let obj = document.getElementById('blah');
    obj.innerHTML = "my text string";
}

myCode();

you'd be teaching them good code structure and would avoid the problem :-)

However, if you want to use global code you could instead wrap the code from the JS element in a function as above, or even just in braces, within the Global Extra code.

In reply to Richard Lobb

Re: HTML UI - JS variable declaration fails

by Christian Gasde -
Hi Richard,

thank you very much for your detailed feedback!

We will do it the way you suggested it: " ...get your students to wrap their code in a function."

"... you'd be teaching them good code structure and would avoid the problem."

This is the best strategy and so will will do it that way! :-)

Best Regards from Coburg
Chris