Alert() does not work with input

Hello!
Please, this code was ment to work but it does not.
It should write out by alert() the imputed value but it does not.
Please, could anyone help?
Many thanks!!!

<!DOCTYPE html>
<html>

<head>
<script>
function m(){
var a=getElementById("demo").value;
alert(a);
}

</script>
</head>


<body>
<form>
<input type="text" id="demo" value=""/>
<input type="button" value="click" onclick="m()"/>
</form>


</body>
</html>

You need it to be this.

document.getElementById

Not just “getElementById” :slight_smile: .

1 Like

Also you should really turn that into an eventlistener instead of using onclick :slight_smile: .

<!DOCTYPE html>
<html>

<head>

</head>
<body>
<form>
<input type="text" id="demo" value=""/>
<input type="button" id="submitBtn" value="click"/>
</form>

<script type="text/javascript">
document.getElementById("submitBtn").addEventListener("click", m);
function m(){
var a=document.getElementById("demo").value;
alert(a);
}
  
</script>
</body>
</html>
2 Likes

Ooo,many thanks!!! :blush:

Hello!
Now I try to split it into arrayelements, like:

<!DOCTYPE html>
<html>

<head>
<script>
function m(){
var a=document.getElementById("demo").value;
var array=JSON.parse("[" + a + "]");
alert(array[0]);
}

</script>
</head>


<body>
<form>
<input type="text" id="demo" value=""/>
<input type="button" value="click" onclick="m()"/>
</form>


</body>
</html>

What is wrong now???
Many thanks!!!

What is this?

JSON.parse(); is the correct version. But this is wrong too.

function m(){
   var a=document.getElementById("demo").value;
   var array=a.split('');
   alert(array[0]);
}

You got rid of my eventlistener; why?

Hello, we must do it without it for beginning. But saved it for later, THANKS!!! :thumbsup:

Would that solve that ? I am nt that far at moment, but I try…

THIS is still not working…???

<!DOCTYPE html>
<html>

<head>
<script>
function m(){
var a=document.getElementById("demo").value;
var array = JSON.parse("[" + string + "]");

alert(array);
}

</script>
</head>


<body>
<form>
<input type="text" id="demo" value=""/>
<input type="button" value="click" onclick="m()"/>
</form>


</body>
</html>

:cold_sweat:

See my edited post.

THANK, that is it!!!
THANK U ALL! Please wish me luck now!!! :sunny:

Hello!
Why the code does not work now?
I just added another form, but this time using name instead of id???
Many thanks!!!

<!DOCTYPE html>
<html>

<head>
<script>
function m(){

var c=document.getElementByName("marc").value;
var a=document.getElementById("demo").value;
var array = a.split('');


var a2=array.length;
alert(a2);
alert(array[2]);

alert(c);
}

</script>
</head>


<body>
<form>
<input type="text" name="marc" value=""/>
<input type="text" id="demo" value=""/>
<input type="button" value="click" onclick="m()"/>
</form>


</body>
</html>

You need getElements (with the s)

var c=document.getElementsByName("marc").value;

Don’t you need this because its a collection?

var c=document.getElementsByName("marc")[0].value;
1 Like

Yes - you always need to specify which element in the collection you are referencing.

The OP is obviously taking a history class as otherwise they’d be placing the JavaScript where it belongs just before the tag, the onclick would be in the JavaScript and not jumbled with the HTML and they’d be using the console for debugging instead of alert.

1 Like

Hello, All!
It assumes the code still “knows” I will input a string (no matter how long) but HOW ? Who “told” him that?

Many thanks!!!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.