How can convert the JQuery code to JavaScript?
A few points:
$(selector)
is closer todocument.querySelectorAll(selector)
than todocument.querySelector(selector)
. So you'll want the former, and to iterate over the returned NodeList.- jQuery
.val( value )
will set the.value
property when called on an object that points to an HTMLElement that has such property (like your<input>
). So that what you want to set, not the.innerHTML
which wouldn't have any positive effects on this element. .text()
is also a jQuery method. As a getter it's equivalent toNode.textContent
.
document.querySelectorAll(".dropdown-menu-state .dropdown-item") .forEach( function( elem ) { elem.addEventListener("click", function() { document.getElementById("state").value = this.textContent; }); });
<input type="text" id="state" data-toggle="dropdown" placeholder="State"><div class="dropdown-menu dropdown-menu-state" aria-labelledby="state" role="menu"> <a href="#" class="dropdown-item">value1</a> <a href="#" class="dropdown-item">value2</a> <a href="#" class="dropdown-item">value3</a></div>
Convert jQuery dropdown change event handler into pure JavaScript code
The event listeners are added using addEventListener and instead of show
/hide
, the display
property of the element is used.
Also, read about ternary operator.
// Event listener that fires when the DOM is loaded
document.addEventListener("DOMContentLoaded", function(){
// Get the elements
let howDidYouFindUs = document.querySelector("#how-did-you-find-us")
let findOther = document.querySelector(".find-other")
// Change event listener
howDidYouFindUs.addEventListener("change", function(){
// Ternary operator to toggle the display
findOther.style.display = howDidYouFindUs.value === "Other" ? "block" : "none"
})
})
Working snippet:
// Event listener that fires when the DOM is loaded
document.addEventListener("DOMContentLoaded", function(){
// Get the elements
let howDidYouFindUs = document.querySelector("#how-did-you-find-us")
let findOther = document.querySelector(".find-other")
// Change event listener
howDidYouFindUs.addEventListener("change", function(){
// Ternary operator to toggle the display
findOther.style.display = howDidYouFindUs.value === "Other" ? "block" : "none"
})
})
<p class="cart-attribute__field">
<label>How did you hear about us?</label><br>
<select required class="required" id="how-did-you-find-us" name="attributes[How did you hear about us?]">
<option value="Search (Google)">Search (Google)</option>
<option value="Event">Event</option>
<option value="Friend / Co-worker">Friend / Co-worker</option>
<option value="Current Customer">Current Customer</option>
<option value="Social Media">Social Media</option>
<option value="Ad">Ad</option>
<option value="News">News</option>
<option value="Other">Other</option>
</select>
</p>
<p class="cart-attribute__field find-other" style="display: none;">
<label for="how-did-you-find-us-other">Other: </label>
<input required class="required" id="how-did-you-find-us-other" type="text" name="attributes[How did you hear about us? - Other]" value="{{ cart.attributes[" How Did You Find Us - Other "] }}">
</p>
Related Topics
Stringified Json Has Unwanted Double Quotes
Is There Any Memory Limit for Google Chrome Browser
Click on Pseudo Element Using Selenium
Javascript Check If Values Exist in Array Json
Why React Props Are Passed Undefined to the Child Component
Force Download a Pdf Link Using Javascript/Ajax/Jquery
How to Add a Dynamic Action That Will Redirect to Another Page by Executing JavaScript Code
Pass Parameter from Input to Onclick Method
How to Display Loading Message When an Iframe Is Loading
Getting Null Value in List When Passing by Ajax Call to MVC Controller
How to Get the Name of the Current Windows User in JavaScript
Save Embedded Pdf from Website
Javascript - Auto Click on a Button on Page Load
How Could I Get the Latest Date from an Array of Dates - Javascript/Typescript
Angular 4: How to Read Content of Text File With Httpclient
Javascript on Click Event for Multiple Buttons With Same Class