Posts

Showing posts with the label Javascript

HTML - How to add placeholder to dropdown (select) with selectpicker

First time in my life I needed to add placeholder for dropdown O.o I even didn`t know that it exists. But, anyway, I need to implement this case. I selected selectpicker from bootstrap to have ability to use live searching in dropdown. HTML for adding dropdown: <select class="dropdown" data-live-search="true">     <option class="hide" value="" disabled selected hidden>Placeholder</option>     <option value="option1">Option 1</option> </select>  Javascript: const select = $('select').selectpicker(); $('.dropdown').on('click', function () { $('.dropdown').find('.hide').hide(); });

jQuery UI - Dialog - How to hide autofocus

The simplest way, just add hidden input with autofocus attribute . < input type ="hidden" autofocus > As it said in jQuery UI documentation, it will find this input firstly by priority and move focus on this input. But as this input hidden user does not see this. Choosing priority: The first element with the autofocus attribute The first :tabbable element within the dialog's content The first :tabbable element within the dialog's buttonpane The dialog's close button The dialog itself

JavaScript - How to create select options from array

Define an array with the values you want to populate in the <select> element. var fruits = ["Apple", "Banana", "Orange", "Grapes", "Mango"]; Create a <select> element in your HTML markup. <select id="mySelect"></select> Iterate through the array and create <option> elements for each value, then append them to the <select> element. Here's an example: document.addEventListener("DOMContentLoaded", function () { var fruits = ["Apple", "Banana", "Orange", "Grapes", "Mango"]; var selectElement = document.getElementById("mySelect"); for (var i = 0; i < fruits.length; i++) { var option = document.createElement("option"); option.value = fruits[i]; option.text = fruits[i]; selectElement.appendChild(option); } }); In this example, the JavaScript code runs ...

Javascript - How to build URL with query parameters

If we just need to add GET parameters to current URL: Firstly, let`s create URL object with current link. const url = new URL(window.location.protocol+window.location.host+window.location.pathname); Where: window.location.protocol - https or http; window.location.host - current host; window.location.pathname - current url after host and before query parameter; After that, let`s add necessary query parameters: url.searchParams.set('id', 123); And get URL what we wanted: url.toString(); For example, let`s redirect to new URL: window.location.href = url.toString();

Javascript - jQuery - Make menu item Active automatically

There is, as minimum, two ways how to add 'active' class to menu item, to highlight it. First one is on backend side. Second one is on frontend side. Now, I talk about variant with frontend side. So, I choose javascript to decide to add 'active' class to menu item or not. There is a code for this: var pathName = window.location.pathname; // Iterate through each anchor tag within list items $("ul li a").each(function () { // Check if the href attribute matches the current URL if ($(this).attr("href") == pathName) { // If there is a match, traverse up the DOM hierarchy to find the corresponding list item $(this).parents('li').each(function () { // Add the 'active' class to highlight the menu item $(this).addClass("active"); }); } }); This script essentially loops through all anchor "a" tags within list items "li" in the specified unordered list "ul". For each a...

JavaScript - jQuery - How to get array values from multiple select

There are various ways to retrieve the selected values from a "<select>" element in jQuery.  The simplest method involves using the "val()" function, like so: $('select').val();  However, this straightforward approach has its limitations, particularly when it comes to manipulating the selected data.  If you require more control and flexibility in handling the selected options, you can leverage the power of the map function: $('select option:selected').map((index, option) => option.value).get(); In this enhanced method, the map function allows you to iterate through the selected options, providing you with greater control over the extraction process.  The addition of the ":selected" filter in the selector ensures that only the chosen options are considered, enhancing the precision of your selection process. This proves invaluable when dealing with scenarios where you need to perform specific actions or manipulations based on the sel...