5

In javascript, I was creating new image tags and I was adding attributes to them with the setAttribute() method but I found that if I add an onclick event and add a function, I can't set in a parameter for it as seen below

count = 0; //Will be the number that will go into parameter for function
function start() {
imageTag = document.createElement("IMG"); //Creates image tag
imageTag.setAttribute("src", "popo.jpg"); //sets the image tags source
count++; //as the start function keeps getting called, count will increase by 1 and the parameter for each function in each individual image tag will increase by one
imageTag.setAttribute("onclick", "disappear(count)"); //this will add the onclick attribute with the function disappear() and the parameter inside which will be a number that will increase as the start function is called again and the image tag is called again *ERROR*
document.body.appendChild(imageTag); //appends the image tag created
}

The problem is, when each new image tag is created, it literally just creates

<img src = "popo.jpg" onclick = "disappear(count)"/>

I wanted it to be more like

<img src = "popo.jpg" onclick = "disappear('1')"/>
<img src = "popo.jpg" onclick = "disappear('2')"/> //and so on...

4 Answers 4

10

add count in function as param, not as string.

count = 0; //Will be the number that will go into parameter for function
function start() {
    imageTag = document.createElement("IMG"); //Creates image tag
    imageTag.setAttribute("src", "popo.jpg"); //sets the image tags source
    count++; //as the start function keeps getting called, count will increase by 1 and the parameter for each function in each individual image tag will increase by one
    imageTag.setAttribute("onclick", "disappear("+count+")"); //this will add the onclick attribute with the function disappear() and the parameter inside which will be a number that will increase as the start function is called again and the image tag is called again *ERROR*
    document.body.appendChild(imageTag); //appends the image tag created
}
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way to not use setAttribute and just use imageTag.onClick = disappear(count)? I tried a lot of different combinations using " " and ' ' and ` ` and eval and JSON.stringify to no avail.
The above answer by @user3227295 is good. Here's a hint if the parameter you're passing in through 'count' is actually a string: imageTag.setAttribute("onclick", "disappear("+count+")"); Should be: imageTag.setAttribute("onclick", "disappear('"+count+"')"); (i.e. wrap the "+count+" in an extra set of single quotation marks).
7

Instead of adding click event as an attribute add it using addEventListener

imageTag.addEventListener("click", disappear.bind(null, count));

Bind will make count available to the function when invoked.

Then create a disappear function

function disappear(ID){
  alert(ID); // This should give the count.
}

Comments

5

Since you use setAttribute, you are setting an event handler content attribute. The corresponding event handler is set to an internal raw uncompiled handler. The scope of that code will be

  • The global object
  • The document
  • The form owner (if any)
  • The element (if any)

If your count variable isn't available in any of these scopes, it won't work. And even if it's available, since you keep increasing it, the used value will be the modified one.

Bu you don't want that. Instead, you can:

  • Write the variable in the uncompiled handler:

    imageTag.setAttribute("onclick", "disappear(" + count + ")");
    
  • Use event handler IDL attributes, e.g.

    var localCount = count; // Using a local copy in case `count` is modified
    imageTag.onclick = function() {
        disappear(localCount);
    };
    

Comments

2

Change "" by '' in parameter function.

before::

imageTag.setAttribute("onclick", "disappear(" + count + ")");

after::

imageTag.setAttribute("onclick", "disappear('" + count + "')");

best practice::

imageTag.setAttribute("onclick", `disappear('${count}')`);

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.