23
Views
11
Comments
Using the getElementBy*** on javascript.

Hi Guys,

I've just started learning responsive web designs and javascript.

I recently hit a snag with using the 'getElement' or DOM.

whenever I try to get an element and equate it to a variable, it returns an error when I run the page on a browser's console.

something about the variable being null, and I can't get the item into the new variable.


Example:

<h1 id = "tera" > Welcome! </h1>      //on HTML file//

var pen =document.getElementById("tera");    // javascript file//

alert(pen);       // javascript file// 


It returns as null every time.

Hey Oludare ,

Are you using the JS in OnInitialize ?

Thanks & Regards,

Sudip Pal

And also have you inspect in browser that the id of the h1 tag is "tera"?

Thanks

Sudip Pal

UserImage.jpg
Oludare Aregbesola

I'm not sure I know what that is.

Like I said, I've just started, and I've done everything possible to know what's wrong but I still keep getting the null error.

However, it works perfectly on the video I'm using to learn.

I use notepad++ and Visual studios for now.

Thank you.

UserImage.jpg
Oludare Aregbesola

I set it to tera myself, with the aim of learning about the use of DOM.

Hey,

If you are not using Outsystems and you are using a normal compiler for JS and HTML then don't forget to add the script file into the HTML file.

Thanks & Regards,

Sudip Pal

UserImage.jpg
Oludare Aregbesola

Check the code simplified, the error message and my javascript file.

er msg.jpg
html code.jpg
jscript.jpg

document.getElementById("demo")

Check the spelling


Hey Oludare , 

Is this problem of yours resolved ?

Thanks & Regards,

Sudip Pal

Champion

Hi Oludare,

Are you creating h1 tag in outsystems or at any others tools?

Best

Arun

UserImage.jpg
Oludare Aregbesola

I'm learning about DOM. and I only used the h1 tag as an example.

but whenever i try to get an element, either by ID/TagName, it results null when put it on the alert box, after equating it to a variable. 

Hi Oludare,

The OutSystems platform automatically assigns generated id's for every OutSystems widget that has its Name property assigned.

You can get the Id automatically generated by OutSystems at runtime and inject it in your JavaScript code, as input parameter.

So add to your JavaScript node, an input parameter called WidgetId and set its value in your action to Input_Textvar3.Id.

Regards,

Daniel

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.