Find class or id name using Javascript

Find class or id name using Javascript

  

Hello,

I am trying to use javascript to get the current date and time and display it in an expression. I have placed the function in the webscreen javascript window and called it from the expression. When I place an alert, the date and time appear correctly. However, when I try to display it normally, it doesn't appear. This is my code

SyntaxEditor Code Snippet (From the webscreen javascript editor)

function getDateTime(inputId){
    var d = new Date();
    var element = document.getElementByClassName(inputId);
    if(element != null){
        element.value = d;
    }else{
        alert("Empty");
    }
}

From the expression 

SyntaxEditor Code Snippet

"<script language='javascript'>
    getDateTime("+DateTitle.Id+");
</script>"

Please help

Hi Amos,

I can see some "problems" with your code.

1. 

var element = document.getElementByClassName(inputId);

This will search for ClassName instead of element Id.

2.

getDateTime("+DateTitle.Id+");

Here, you have to pass the id as string in order to concatenate in your funcion.

try: 

getDateTime('"+DateTitle.Id+"');

Summary:

changes to make on your function:

var element = document.getElementById(inputId);

changes to make on your expression:

getDateTime('"+DateTitle.Id+"');


Hope it helps.

Cheers


Hi Amos,

Following Nuno's answer, I share with you an oml with a possible solution.

Regards,
Thomas

prolly you need to add a # to it


getDateTime('#"+DateTitle.Id+"');

check this fiddle out:

https://jsfiddle.net/7v4eahk6/1/


Also, jQuery makes life much easier all around.

J.Ja