0

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

4 Answers 4

1

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

  })
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your answer the a="" loop is working on any click a="click" is not working
which loop are you talking about?
alert(a); when i do it just a=""; when i click i get a = "click open"; not working
0

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>

2 Comments

thank you, your examples are very good, I get value when clicked, but no value when not clicked
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);`
0

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>

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.