Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Add <code>onclick="change()"</code> to the element. </p> <p>Edit:</p> <pre><code>function change(x) { if(x=="something"){ var target = document.getElementById("target"); if (y == "imgA") {target.className = "cast1";} else if (y == "imgB") {target.className = "cast2";} else if (y == "imgC") {target.className = "cast3";} else if (y == "imgD") {target.className = "cast4";} else {target.className = "chart";} } } </code></pre> <p>Alternately if you mean to only allow the function to execute when called by a particular element you can check the element. The <code>this</code> reference will refer to the element that owns the function.</p> <pre><code>function change(x) { if(this.id == "target") var target = document.getElementById("target"); if (y == "imgA") {target.className = "cast1";} else if (y == "imgB") {target.className = "cast2";} else if (y == "imgC") {target.className = "cast3";} else if (y == "imgD") {target.className = "cast4";} else {target.className = "chart";} } } </code></pre> <p>Edit2:</p> <p>Normally you would bind using <code>element.onclick</code> however when using the inline declaration the <code>this</code> reference refers to the window.</p> <p>Details here <a href="http://www.quirksmode.org/js/this.html" rel="nofollow">http://www.quirksmode.org/js/this.html</a></p> <p>Without changing your code too much you can change your function slightly as suggested below.</p> <pre><code>function change(element,x) { if(element.id=="target"){ var target = document.getElementById("target"); if (y == "imgA") {target.className = "cast1";} else if (y == "imgB") {target.className = "cast2";} else if (y == "imgC") {target.className = "cast3";} else if (y == "imgD") {target.className = "cast4";} else {target.className = "chart";} } } </code></pre> <p>Then change your inline onclick to pass a reference to the element. <code>onclick="change(this,'target')"</code></p> <p>Generally its considered bad practice to use inline onclick declarations. So consider doing your click binding in script.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload