Get set div value

 If you want to concatenate the value of a clicked div with the previously clicked div's value when clicked for the second time, you can achieve this by storing the previous value and then concatenating it with the current value. Here's how you can do it:


HTML:

```html

<div id="container">

    <div class="clickable">Div 1</div>

    <div class="clickable">Div 2</div>

    <div class="clickable">Div 3</div>

</div>

```


JavaScript:

```javascript

var previousClickedValue = null;


document.getElementById('container').addEventListener('click', function(event) {

    if(event.target && event.target.classList.contains('clickable')) {

        var clickedDivValue = event.target.textContent;

        

        if (previousClickedValue !== null) {

            clickedDivValue = previousClickedValue + clickedDivValue;

        }

        

        console.log("Clicked div value: " + clickedDivValue);

        

        previousClickedValue = clickedDivValue;

    }

});

```


In this code, we introduce a variable `previousClickedValue` to store the value of the previously clicked div. Upon clicking a div, if `previousClickedValue` is not null (meaning it's not the first click), we concatenate the previous value with the current value. Then, we log the concatenated value to the console and update `previousClickedValue` with the new value. This way, you can concatenate the values of clicked divs on the second click and onwards.

Previous
Next Post »