Home > Enterprise >  javascript div onclick if else control
javascript div onclick if else control

Time:12-18

First of all, my English is very bad, sorry for that. I don't know much javascript and I would like your help.

there is one div

    <div id="click"></div>

when clicking on this div

if (...click){
var a = "click open";
}else{
var a = "";
}

I need a coding like this. When clicked, the variable a should be filled and empty when not clicked. how do i do this please help all i want is when it is clicked the variable a will be full when not clicked it will still work but it will be empty

if (...click){
var a = "click open";
}else{
var a = "";
}

all i want is when it is clicked the variable a will be full when not clicked it will still work but it will be empty

CodePudding user response:

Considering the following html :

<div id="click"></div>

You can initialize the variable in javascript as an empty string and add an event listener to change the value when the div is clicked with the following js code:

let a = "";
const click = document.getElementById("click");

click.addEventListener('click', function(){

    a = "click open";

  })

CodePudding user response:

I don't understand your question, because your button is empty here. So, if your button is empty, how do you click on the button and get the result? That is why I added two answers and hope it will help you.

//example one
const clickBtn = document.getElementById("click");
let click = true;
if (click) {
  var a = "click open";
  clickBtn.innerHTML = a;
} else {
  var a = "";
  clickBtn.innerHTML = a;
}

//exmaple two 
const clickBtn2 = document.getElementById("click2");
let click2 = true;
clickBtn2.addEventListener("click", () => {
if (click2) {
  var a = "yes, click open";
  clickBtn2.innerHTML = a;
} else {
  var a = "";
  clickBtn2.innerHTML = a;
}
});
<div id="click"></div>
<div id="click2">Click me</div>

CodePudding user response:

I added the text "Click" to give the element a clickable area.

let a = '';

document
  .querySelector('#click')
  .addEventListener('click', () => a = 'clicked');
<div id="click">Click</div>

CodePudding user response:

this is what i wanted to do i didn't know how to do it

            let jmin = "";
        const click = document.getElementById("price_range");
        click.addEventListener('click', function(){
        var jmin = "min="   minimum_price;
        })
        history.pushState(null, '', 'index.php?'   kategori   filtb   filtr   filts   jmin);
  • Related