I've Searched The Web So Much But For Some Reason, My Code = No Work;
My JavaScript:
let a = document.getElementById("a");
let b = document.getElementById("b");
let c = document.getElementById("c");
let button = document.getElementById("button");
button.onclick = function() {
var info = {"a": a.value, "b": b.value, "c": c.value};
console.log(info.a info.b info.c);
}
Everything Except The C One Works, Which Is A Dropdown(A And B Are Textboxes).
HTML:
<button type = "button" id = "button">Button</button>
<input type = "text" id = "a">
<br><br>
<input type = "text" id = "b">
<br><br>
<select name = "c" class = "c">
<option value = "c1" class = "c" selected>c1</option>
<option value = "c2" class = "c">c2</option>
<option value = "c3" class = "c">c3</option>
CodePudding user response:
Your HTML has problems.
<input type = "text" id = "a">A</input> is wrong, it should be like this: <input type = "text" id = "a" value="A"/> and only then you will get the "value" of this field. Same holds true for your second text box. Also add "id" to the select box.
CodePudding user response:
- you are missing an closing quote for id = "button
- There is no id named c .
CodePudding user response:
First issue is that <select name = "c" class = "c"> you have to make it id instead of class.
Also Button HTML is bit wrong <button type = "button" id = "button>Button</button> instead it should be <button type = "button" id = "button">Button</button>
After that it should work. If it still does not work then you can wrap JS code in window.onload function.
window.onload = function() {
let a = document.getElementById("a");
let b = document.getElementById("b");
let c = document.getElementById("c");
let button = document.getElementById("button");
button.onclick = function() {
var info = {"a": a.value, "b": b.value, "c": c.value};
console.log(info.a info.b info.c);
}
}
CodePudding user response:
I'll just cover some of the points the others have raised first.
Missing quote mark on
id="buttonwhich makes your HTML invalid.No
idon theselectelement.No
</select>tag.Having the same class on the options as well as the
selectseems odd.The text content you're ascribing to
Ashould be added as avalueattribute to the element, and there's no reason to have a</input>tag.
So I've removed all those issues.
I've switched out all the ids for classes to make things consistent, and use
querySelectorto pick up those elements. I removed the button ID altogether.I've used
addEventListeneron the button instead ofonclickwhich is a slightly more modern approach for dealing with events.
let a = document.querySelector('.a');
let b = document.querySelector('.b');
let c = document.querySelector('.c');
let button = document.querySelector('button');
button.addEventListener('click', handleClick, false);
function handleClick() {
const info = { a: a.value, b: b.value, c: c.value };
console.log(info.a info.b info.c);
}
<button type="button">Button</button>
<input type="text" value="A" >
<br><br>
<input type="text" >
<br><br>
<select name="c" >
<option value="c1" selected>c1</option>
<option value="c2">c2</option>
<option value="c3">c3</option>
</select>
