Mustafa Ulusan
Mustafa Ulusan

Reputation: 9

javascript div onclick if else control

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

Upvotes: 0

Views: 73

Answers (4)

Mustafa Ulusan
Mustafa Ulusan

Reputation: 9

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);

Upvotes: 0

Robert
Robert

Reputation: 10380

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>

Upvotes: 0

NAYMUR
NAYMUR

Reputation: 81

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>

Upvotes: 0

Simon Leroux
Simon Leroux

Reputation: 477

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";

  })

Upvotes: 1

Related Questions