object methods
For example:
const user = {
value: document.getElementById('input')
}
function showValue () {
alert(user.value);
}
I want to call the user input using user object and value property and alert the user input by clicking on the submit button
Can anyone help me with that?
document .getElementById('submitButton') .addEventListener('click', showValue);
pass user to showValue() while you are at it
It doesn't work It still shows null to be clear, I'm using a button not a submit input and I have an onClick event inside my HTML document = my function and I think using an event inside my JS code is not a good idea
Well then the element doesn't exist when you create the user object
need to use document onready
Or move the script tag below the element
This is my HTML code <input type="text" id="input"> <button id="submit" onClick="showValue()">Click me</button> And this is my external JS code let user = { value: document.getElementById('input') } function showValue () { alert(user.value); } But I get null And I think the browser doesn't care about my input tag
Where is your external JS code <script> tag?
Try user.value.value
Обсуждают сегодня