Swap out the string variable when it is modified

To generate a string inside the "code" variable that combines the selected image values, the final code should appear similar to: "test1/A=1a/B=1b" or "test2/A=1b/B=1a", etc. If the user modifies icon "A," it should replace the value in the code instead of adding to it like in the example below.

Please refer to the snippet below for more details.

let code;

$(".selectIco").click(function(){
  $(".iconSelect").html("");
  $(this).clone().appendTo(".iconSelect");
})

let thisIcon;
$(".clone").on("click", function() {
  thisIcon = $(this);
  $(".iconSelect").html("");
    $(".icons").slideDown("1000");

function imagePicker() {
  $(".selectIco").on("click", function() {
    $(".iconSelect").html("");
    $(this).clone().appendTo(".iconSelect");
    $(thisIcon).html("");
    $(".iconSelect img").clone().appendTo(thisIcon);
  })
}
imagePicker();
    })

$(".test1").click(function(){
  code = "test1";
  console.log(code);
})
$(".test2").click(function(){
  code = "test2";
  console.log(code);
})

//code
  $('.cloner1').bind("DOMSubtreeModified",function(){
    if ($(this).children("img").length > 0 ) {
      iconA = $(this).children('.selectIco').attr('value');
      code += 'A=' + iconA;

      console.log("export of code = " + code);

    }
  });
    $('.cloner2').bind("DOMSubtreeModified",function(){
    if ($(this).children("img").length > 0 ) {
      iconA = $(this).children('.selectIco').attr('value');
      code += 'B=' + iconA;

      console.log("export of code = " + code);

    }
  });
.iconSelect {
  height: 90px;
  width: 90px;
  border: 2px solid blue;
}
.clone {
  float:left;
  margin-right: 10px;
  height:120px;
  width: 120px;
  background-color: pink;
}
.icons {
  display: none;
}
.test1, .test2 {
  background-color: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>first</h1>
<div class="test1">clickme</div>
or
<div class="test2">clickme</div>
<br/>

<h1>second</h1>
<div class="clone cloner1">A</div>
<div class="clone cloner2">B</div>

<div class="iconSelect"></div>
<div class="icons">
  <img  class="selectIco" src="https://upload.wikimedia.org/wikipedia/en/thumb/9/9f/Twitter_bird_logo_2012.svg/100px-Twitter_bird_logo_2012.svg.png" value="/1a">
  <img class="selectIco" src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/ab/Gmail_Icon.svg/100px-Gmail_Icon.svg.png" value="/1b">
</div>

Answer №1

Make a simple adjustment to your code by changing the assignment operator from += to = in the last two instances, and everything should work smoothly:

let code;

$(".selectIco").click(function() {
  $(".iconSelect").html("");
  $(this).clone().appendTo(".iconSelect");
})

let thisIcon;
$(".clone").on("click", function() {
  thisIcon = $(this);
  $(".iconSelect").html("");
  $(".icons").slideDown("1000");

  function imagePicker() {
    $(".selectIco").on("click", function() {
      $(".iconSelect").html("");
      $(this).clone().appendTo(".iconSelect");
      $(thisIcon).html("");
      $(".iconSelect img").clone().appendTo(thisIcon);
    })
  }
  imagePicker();
})

$(".test1").click(function() {
  code = "test1";
  console.log(code);
})
$(".test2").click(function() {
  code = "test2";
  console.log(code);
})

//code
$('.cloner1').bind("DOMSubtreeModified", function() {
  if ($(this).children("img").length > 0) {
    iconA = $(this).children(".selectIco").attr("value");
    code = "A=" + iconA;

    console.log("export of code = " + code);

  }
});
$('.cloner2').bind("DOMSubtreeModified", function() {
  if ($(this).children("img").length > 0) {
    iconA = $(this).children(".selectIco").attr("value");
    code = "B=" + iconA;

    console.log("export of code = " + code);

  }
});
.iconSelect {
  height 90px;
  width: 90px;
  border: 2px solid blue;
}

.clone {
  float: left;
  margin-right: 10px;
  height: 120px;
  width: 120px;
  background-color: pink;
}

.icons {
  display: none;
}

.test1,
.test2 {
  background-color: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>first</h1>
<div class="test1">clickme</div>
or
<div class="test2">clickme</div>
<br/>

<h1>second</h1>
<div class="clone cloner1">A</div>
<div class="clone cloner2">B</div>

<div class="iconSelect"></div>
<div class="icons">
  <img class="selectIco" src="https://upload.wikimedia.org/wikipedia/en/thumb/9/9f/Twitter_bird_logo_2012.svg/100px-Twitter_bird_logo_2012.svg.png" value="/1a">
  <img class="selectIco" src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/ab/Gmail_Icon.svg/100px-Gmail_Icon.svg.png" value="/1b">
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Oops! You forgot to include the necessary getStaticPaths function for dynamic SSG pages on '/blogs/[post]'

Whenever I attempt to execute npm run build, an error occurs. The following build error occurred: Error: getStaticPaths is required for dynamic SSG pages and is missing for '/blogs/[post]'. This is the code snippet causing the issue: function ...

"What is the best approach for setting up an Azure Web App to host both an Express static site and API

Setting up an Express app was a breeze for me, but when it comes to deploying it on Azure Web App, I'm hitting some roadblocks! The structure of my app is quite simple: a static web app with its own API. Requests to /website.com/api are forwarded to ...

Detect and switch the value of a CSS selector using Javascript

Incorporating the subsequent jQuery code to insert CSS styles into a <style id="customizer-preview"> tag within the <head>: wp.customize( 'site_title_color', function( value ) { value.bind( function( newval ) { if ( $( &a ...

Transforming Poloniex API Callback JSON into a compatible format for Highcharts.Stockchart

I am currently working on a project that involves retrieving JSON data from Poloniex's public API method (specifically the returnChartData method) to generate a graph using Highchart Stockchart. The graph would display the historical performance of va ...

Stopping all animations with JQuery animate()

I have a question about stopping multiple animations. Here's some pseudocode to illustrate my situation: CSS #div1 { position: absolute; background-image: url("gfx/cat.jpg"); width: 60px; height: 70px; background-size: 50%; b ...

When using a routerlink in an a tag with Bootstrap 4, the navigation tab functionality may not work as expected

I've been working on bootstrap4 tabs and everything is working smoothly until I add a router link to the anchor tag. Strangely, only the hover effect works in this case. Can anyone help me troubleshoot this issue? Below is my code snippet: <link ...

Element on webpage "Expands" When Scrolled into Visibility

After posting a question on Stack Overflow, I noticed discrepancies between the element dimensions reported by Chrome Inspector and Selenium WebDriver. While Chrome Inspector showed w = 979, h = 1961, Selenium returned dimensions of 979 and 1461 respective ...

Implement responsive data tables by setting a specific class for hiding columns

Having trouble assigning a specific class name to individual columns in datatables? It seems that when columns are hidden using the responsive extension, the desired class is not applied. Looking for a solution or workaround. Check out this example from D ...

Conditional Matching with Javascript Regular Expressions

My strings are formatted like this: #WTK-56491650H #=> want to capture '56491650H' #M123456 #=> want to capture 'M123456' I am trying to extract everything after the # character, unless there is a dash. In that case, I onl ...

I am encountering an issue with my register button not being able to submit in PHP+AJAX, while the

Having trouble with the registration page in PHP and AJAX. The login section works fine as I can sign in, but the registration button appears disabled or inactive, preventing form submission. I suspect an error somewhere, but unable to pinpoint it. Login ...

Transferring request body data between routes in Express: A guide

In my MERN application, there are two specific routes in place. The first route is designated for 'Storing Data' and is referred to as the 'data' route. The second route is used for 'Register User' functionalities and is known ...

Tips on submitting JSON data to a server

I need the data to be structured like this {"email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7f0c3f18121e1613511c1012">[email protected]</a>","password":"1"} but it is currently appearing like this { & ...

Optimal method for managing errors in Flask when handling AJAX requests from the front end

I'm currently working on a React application that communicates with a Python Flask server. One of the features I am adding allows users to change their passwords. To do this, an AJAX request is sent from React to Flask, containing both the old and ne ...

Interactive CSS Video Gallery Slideshow

Is there a way to create a slideshow video gallery using CSS? I've looked everywhere but all I find are slideshow image galleries. If anyone has any resources or tips on how to do this, please share! This is the kind of video gallery I'm looking ...

Unlocking the Secrets of Laravel Blade Integration in a Script File

I am attempting to create a customized store locator application using the guidance provided in this useful tutorial on Google Maps and Laravel 5. While going through various queries related to integrating Google Maps with Laravel, I came across these info ...

When using Codeigniter with AJAX dropdown menus, some issues may arise if form validation fails and the view gets redirected or reloaded

After the validation process fails and redirects back to the same controller index, an issue arises where the second dependent dropdown stops functioning. if (!$this->form_validation->run()) { $this->index(); } The form consi ...

Is there a way for me to prioritize the sidebar over the main content on the page

My theme is efficient and visually appealing, but there's one issue with the mobile layout - it displays the sidebar after the content. How can I rearrange this in my theme so that the sidebar appears before the content? Thanks for your help! ...

Failure to Connect HTML with Stylesheet

Feeling a bit lost here. I've been attempting to connect my homepage to my stylesheet, but no matter how often I refresh the page, it just won't recognize the link. I'm diligently following the instructions from the guide I have, copying and ...

What is the best method to verify a string against a set of approved characters using JavaScript?

I have written some code that sanitizes user input to allow only alphanumeric characters and hyphens. Here is the code snippet: https://jsfiddle.net/py4pnr0L/ const inputValue = 'GHJHlk;sxa787BVK'; const sanitizedValue = inputValue.toLowerCase( ...

What is the proper method for utilizing the .done or .complete functions in conjunction with .toggle in jQuery?

I am struggling to understand the proper usage of .complete or .done after .toggle in jQuery. My goal is to have the button's text change after the toggle animation finishes, but I'm not sure if I'm chaining them correctly. The jQuery docume ...