How Can I Get The Content Of An Element In Js? January 29, 2024 Post a Comment I am trying to get the value and content of an option element. So far I have it getting the value using this.value as shown below id='name' onchange='someFunction(this)'> <option selected='selected' disabled='disabled' value=''>CONTENT</option> " . $options . " </select>" CopyfunctionsomeFunction(obj) { var value = obj.value; var content = obj.querySelector("option:checked").textContent; } CopyThat should do it:I changed the object passed in the onchange function. It passes the select object to the function using the keyword this. Then we use value to select the value and a querySelector that selects the selected option using the selector option:checked. This way your code becomes more readable.However you could store it inside the onchange like this:onchange='showAccountInfo(this.value, this.querySelector("option:checked").textContent)'CopyPersonally I wouldn't use (or recommend) the use of inline events.I would do it like this using addEventListener:functionsomeFunction(e) { //this refers to the select element (the owner of the event);var value = this.value; var content = this.querySelector("option:checked").textContent; alert("value: " + value + " content: " + content); } document.querySelector("#name").addEventListener("change", someFunction, false); //attach an onchange event using the addEventListener method.//I'm using document.querySelector here to select an element on the page.Copy<selectname='name'id='name' ><optionselected='selected'value='1:'>CONTENT 1</option><optionvalue='2:'>CONTENT 2</option><optionvalue='3:'>CONTENT 3</option><optionvalue='4:'>CONTENT 4</option></select>Copy Share Post a Comment for "How Can I Get The Content Of An Element In Js?"
Post a Comment for "How Can I Get The Content Of An Element In Js?"