JavaScript change div style according to values passed as argument to javascript

here is javascript what i have tried

function toggle(clicked_id,name) {
			alert(clicked_id);
			alert(name);
			var text = document.getElementById("clicked_id");
			var ele = document.getElementById("name");
			alert(ele.style.display);
			if(ele.style.display == "block") {
		    		ele.style.display = "none";
				text.innerHTML = "+";
				text.value="+"
		  	}
			else {
				ele.style.display = "block";
				text.innerHTML = "-";
				text.value="-"
			}
			return
		} 
Answer:1

Just remove quotes around variable name

var text = document.getElementById(clicked_id);
var ele = document.getElementById(name);

DEMO

Answer:2

Issue is this:

var text = document.getElementById("clicked_id"); // func arg is in string format
var ele = document.getElementById("name");  // func arg is in string format

You have to use it without quotes.

var text = document.getElementById(clicked_id);
var ele = document.getElementById(name);

yet there is a better way of doing this, you can do something like this:

<input type="button" id="displayText1" onClick="toggle(this)" value="+" style="margin-left:86%;" name="toggleText1">

<input type="button" id="displayText2" onClick="toggle(this)" value="+" style="margin-left:86%;" name="toggleText2">

just pass this in the argument and you can change the function like this:

function toggle(el) {
    var text = el;
    var ele = document.getElementById(el.name);
    if(ele.style.display == "block") {
        ele.style.display = "none";
        text.innerHTML = "+";
        text.value="+"
    } else {
        ele.style.display = "block";
        text.innerHTML = "-";
        text.value="-"
    }
}

As you don't need to return anything it is just a dom manipulation stuff, so you can remove the return statement.

I suggest you to avoid inline event handlers, so if you are interested in unobtrusive javascript then you can place this event handler in a js file with that toggle() function and you can use it:

document.getElementById('displayText1').addEventListener('click', function(){
    toggle(this);
});
document.getElementById('displayText2').addEventListener('click', function(){
    toggle(this);
});

or with querySelectorAll method:

document.querySelectorAll('#displayText1, #displayText2').addEventListener('click', function(){
        toggle(this);
    });
Answer:3

I have number of different buttons on my website and I want to assign different task to each button, currently I am referencing them one by one using getElementByID and because I have so many buttons ...

I have number of different buttons on my website and I want to assign different task to each button, currently I am referencing them one by one using getElementByID and because I have so many buttons ...

  1. loop through buttons javascript
  2. loop through buttons vba
  3. loop through buttons jquery
  4. loop through buttons
  5. loop through buttons vb.net
  6. loop through buttons java
  7. loop through buttons android
  8. loop through radio buttons javascript
  9. loop through option buttons vba
  10. loop through radio buttons jquery
  11. loop through command buttons vba
  12. c# loop through buttons on form
  13. loop through all buttons jquery
  14. c# loop through buttons
  15. loop through radio buttons
  16. tkinter loop through buttons
  17. wpf loop through buttons
  18. selenium loop through buttons
  19. loop through radio buttons php
  20. excel loop through buttons

Just trying to get the current index of the touchable highlight that is pressed. For some reason it is only returning the highest number in the array. Any ideas? render: function() { var images = ...

Just trying to get the current index of the touchable highlight that is pressed. For some reason it is only returning the highest number in the array. Any ideas? render: function() { var images = ...

  1. react native which language
  2. react native which ide
  3. react native which database
  4. react native what is
  5. react native where is console log
  6. react native what is expo
  7. react native what is flex
  8. react native where is apk
  9. react native where to start
  10. react native what is redux
  11. react native what is props
  12. react native what is state
  13. react native what is ref
  14. react native what is a component
  15. react native where to call api
  16. react native what is promise
  17. react native what is eject
  18. react native what is await
  19. react-native what is constructor
  20. react-native what is flexbox

var a = document.createElement('a'); a.setAttribute('target','_blank'); a.click(); The above code doesn't work for me. Does setAttribute not work if the click event happens in JavaScript?

var a = document.createElement('a'); a.setAttribute('target','_blank'); a.click(); The above code doesn't work for me. Does setAttribute not work if the click event happens in JavaScript?

  1. setattribute('target' '_blank')
  2. setattribute target _blank
  3. javascript setattribute target _blank
  4. form.setattribute( target _blank )
  5. link.setattribute('target' '_blank')
  6. js setattribute target _blank

I have this function, the purpose is to find the maximum (number) element in an array, the value is coming from an argument. I'm having a hard time understanding the logic, well I'm old and slow so ...

I have this function, the purpose is to find the maximum (number) element in an array, the value is coming from an argument. I'm having a hard time understanding the logic, well I'm old and slow so ...

  1. find value array javascript
  2. find value array php
  3. find value array js
  4. find value array python
  5. find value array matlab
  6. find value array
  7. find value array excel
  8. find value array java
  9. find value array typescript
  10. find value array c#
  11. find value array jquery
  12. find value array vba
  13. find value array numpy
  14. find value array powershell
  15. find value in arraylist
  16. find array value by key php
  17. find array value in mongodb
  18. find array value in string php
  19. find array value by index
  20. find value in array of objects javascript