Photo by Mohammad Rahmani on Unsplash
Enhancing User Experience with JavaScript Techniques for building annoying cookie consent and a form.
Table of contents
No headings in the article.
In web development, it's crucial to create an engaging user experience that seamlessly interacts with the user. By utilizing JavaScript techniques, we can control the appearance of modal pop-ups, dynamically update CSS properties, handle form submissions effectively, and enhance user interactivity. In this article, we'll explore these techniques, providing clear explanations and code snippets along the way.
Controlling Modal Pop-up Appearances with setTimeout: To control when a modal pop-up appears, we can utilize the JavaScript method setTimeout
. By setting a specific delay, we can trigger the appearance of the modal after a specified time has elapsed. This technique ensures that the modal appears at the desired moment, enhancing the user's interaction with the website.
Dynamic CSS Property Updates with element.style: The element.style
property allows us to dynamically update CSS properties of an HTML element. By accessing this property and modifying its values, we can change various visual aspects of an element programmatically. This technique enables us to create interactive interfaces where elements respond to user actions or application logic.
Handling Form Data with FormData and .get(): When working with HTML forms, we can leverage the FormData
object to obtain form data as an object. The FormData
object simplifies the process of retrieving form inputs and their values. By using the FormData.get()
method, we can access specific form data, such as text input, email addresses, and passwords, allowing us to utilize this data within our application logic.
Controlling Form Submissions with event.preventDefault(): To have more control over how we handle form submissions, we can utilize the event.preventDefault()
method. This function prevents the default behavior of form submission, giving us the freedom to handle the form data as needed. With this technique, we can perform custom validation, data manipulation, or AJAX requests before submitting the form to the server.
Preventing User Actions with CSS Flexbox and Class Toggling: Using the CSS flexbox property row-reverse
, we can cleverly prevent users from clicking the decline button until specific conditions are met. By toggling a class with this property, we can control the order of buttons or elements, effectively restricting certain actions until desired criteria are fulfilled.
Disabling Elements with the "disabled" Attribute: To restrict user interaction with certain elements, such as a close button, we can utilize the HTML attribute disabled
. By setting this attribute on an element, we prevent users from interacting with it until we choose to enable it programmatically. This technique allows us to guide users through specific workflows or ensure that certain actions are performed in a specific order.
Conclusion: JavaScript provides powerful tools and techniques for enhancing user experience in web applications. By controlling the appearance of modal pop-ups, dynamically updating CSS properties, handling form data effectively, preventing unwanted user actions, and enabling or disabling elements as needed, we can create interactive and user-friendly interfaces. Implementing these techniques can greatly improve the overall usability and engagement of your web application.
Code Snippets: Here are some code snippets demonstrating the discussed techniques:
setTimeout for cookie Modal Pop-up:
setTimeout(function(){ modal.style.display = 'inline' }, 1500);
- Updating CSS Properties with Element.style:
modalCloseBtn.addEventListener('click', function(){
modal.style.display = 'none'
});
Retrieving Form Data with FormData and .get():
const form = document.getElementById("myForm") const myFormData = new FormData(form) const fullName = consentFormData.get('fullName') const email = formData.get("email") const password = formData.get("password")
Handling FormSubmission with event.preventDefault():
form.addEventListener('submit', function(e){ e.preventDefault()
- Preventing User Actions with CSS Flexbox and Class Toggling:
html:
<div class="modal-choice-btns" id="modal-choice-btns">
<button type="submit" class="modal-btn">Accept</button>
<button class="modal-btn" id="decline-btn">Decline</button>
</div>
CSS:
.modal-choice-btns{
margin-top: 10px;
padding: 8px 16px;
display: flex;
justify-content: center;
}
.modal-btn{
padding: 10px 24px;
cursor: pointer;
}
.modal-btns-reverse{
flex-direction: row-reverse;
}
Javascript:
const declineBtn = document.getElementById('decline-btn')
const modalChoiceBtns = document.getElementById('modal-choice-btns')
declineBtn.addEventListener('mouseenter', function(){
modalChoiceBtns.classList.toggle('modal-btns-reverse')
})
Disabling Elements with the "disabled" Attribute:
<button id="modal-close-btn" disabled>X</button>
Feel free to use these code snippets in your web development projects to implement the discussed techniques effectively.