How Do I Concat The Values Of Select And Input Field And Also Display Altogether With Javascript March 27, 2024 Post a Comment I'm making a phone picker input menu which has a and field just like below: Solution 1: You can do something like thisconst select = document.querySelector('#country'); const tel = document.getElementById('tel'); let prevVal = select.value; // storing previous value of select// this block will be exected on init of codeif(tel.value) tel.value = `${select.value}${tel.value}`; else tel.placeholder = `${select.value}${tel.placeholder}`; // adding event on changing input tel.addEventListener('change', ({ target }) => { const reg = newRegExp(`(^${prevVal} )`); // checking do we already have country codeif (reg.test(target.value)) tel.value = tel.value.replace(reg, target.value); else tel.value = `${select.value}${target.value}`; }); // adding event on changing select select.addEventListener('change', ({ target }) => { const reg = newRegExp(`(^${prevVal})`); if(tel.value) tel.value = `${target.value}${tel.value.replace(reg, '')}`; else tel.placeholder = tel.placeholder.replace(reg, target.value); prevVal = target.value; });Copy<selectid="country"><optiondata-countryCode="IN"value="91"selected>India</option><optiondata-countryCode="US"value="1">US</option><optiondata-countryCode="GB"value="44">UK</option></select><inputtype="tel"placeholder ="956 826 4457"id="tel">CopySolution 2: First of all there are some wierd details in the code you provided.<select id-"country"> Copysupposes to be <select id="country"> Copy?why do you concat templates with plus using templates at the same time? isn't it enough just to usetel.value = ${select.options[select.selectedIndex].value}${tel.value}?In addition, what event are you going to use? If we select country for the first time it works well, adding '91' for example. But next time (if we picked up the wrong country accidentally) it will add the code again.As for the problem , could you give more detailed explanation of what happens now exactlySolution 3: The problem is that you have put a '-' instead of an '=' sign on the first line. It should be:<select id="country"> CopyAlso, why have you used jQuery? Javascript covers it just as easily:constselect = document.getElementById('country'); const tel = document.getElementById('tel'); tel.value = select.options[select.selectedIndex].value + tel.value; Copy Share Post a Comment for "How Do I Concat The Values Of Select And Input Field And Also Display Altogether With Javascript" Top Question Show/hide Div Section Based On Where The Page Is Redirected From? I have two php files which redirect like this `header('… SetInterval Slows Down With Tab/window Inactive I build a web app and I use setInterval with 500ms timer fo… Compare Two Arrays Based On Length: Skip Empty Values I have a form with 4 input (can be even many more) where th… Weird Behavior Of Long Presses In Chrome And Firefox For Android So, I found some weird behavior of long presses in Chrome a… JavaScript: How To Force Image() Not To Use The Browser Cache? If I load the nextimg URL manually in the browser, it gives… How Do Js Animations Work? Im trying to understand how to make a javascript animation … Extracting Data From Json Object In JQuery Or JS I want to use the currency data provided by https://raw.git… Show/hide Form Contents Based On Radio Selections Im building a form that has conditional fields that need to… JavaScript Selector By Class Prefix? in CSS, we can select multiple classes with same prefix lik… JQuery - Renaming Checkboxes Sets Consecutively I'm trying to build a form to allow multiple elements (… December 2024 (1) November 2024 (40) October 2024 (69) September 2024 (19) August 2024 (217) July 2024 (200) June 2024 (430) May 2024 (699) April 2024 (479) March 2024 (930) February 2024 (970) January 2024 (882) December 2023 (877) November 2023 (398) October 2023 (557) September 2023 (288) August 2023 (334) July 2023 (272) June 2023 (379) May 2023 (231) April 2023 (134) March 2023 (142) February 2023 (163) January 2023 (287) December 2022 (132) November 2022 (231) October 2022 (175) September 2022 (162) August 2022 (501) July 2022 (304) June 2022 (313) Menu Halaman Statis Beranda © 2022 - JavaScript Guide