How Do I Concat The Values Of Select And Input Field And Also Display Altogether With Javascript June 27, 2023 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 Shiny App : Disable Downloadbutton My shiny app produces some files that user can download. I … Capture The Events Of 2 Or More Keyboards In JavaScript/Web Browser I am writing some test applications that perform simple key… D3 - Add A Raw Symbol I have a D3 code. I want to add a svg symbol 'as is… Read The Source Of A Script Tag From The Cache In Firefox I'm adding some error reporting to my application. I wa… If Decimal Value, Convert To Two Decimals AND Dot Separated Value To Comma Separated I currently have values that look like the following: 30 32… HTML Transient Modal Window We have a legacy web application. At various places it open… How To Cancel User Upload In Formidable (Node.js)? I have been working on this problem for two days now, and I… Bootstrap Validation On Dynamically Adding Multiple Fields I am using bootstrap v3.1.1 and I want to validate a form w… If The User Already Logged In Then The First Tab Will Hide And Directly Jump To The Second Tab I have created a tab section without using Jquery UI. Now I… Is There Any Kind Of JQuery.browser Fallback? As jQuery.browser has been removed since 1.9 I am getting a… December 2024 (1) November 2024 (36) October 2024 (71) September 2024 (24) August 2024 (364) July 2024 (343) June 2024 (701) May 2024 (1291) April 2024 (807) March 2024 (1536) February 2024 (1692) January 2024 (1329) December 2023 (1351) November 2023 (386) October 2023 (544) September 2023 (294) August 2023 (358) July 2023 (277) June 2023 (368) May 2023 (222) April 2023 (139) March 2023 (143) February 2023 (185) January 2023 (263) December 2022 (131) November 2022 (240) October 2022 (147) September 2022 (166) August 2022 (496) July 2022 (315) June 2022 (295) Menu Halaman Statis Beranda © 2022 - javascript html php