0

Is there any way to pass the id/name of an html image when you click on it? It seems the onclick function is defunct now, and I can't seem to find anything with a similar functionality.

Each image has a unique id, which is set at creation, by the name variable. This is how I am creating the clickable images

function addbutton(position, name) {
    var element = document.createElement("input");
    var imgURL = chrome.extension.getURL("icons/donate.png");
    element.setAttribute("type", "image");
    element.setAttribute("src", imgURL);
    element.setAttribute("class", "addressbuttons");
    element.setAttribute("id", name); //the name variable is set to a specific text
    element.setAttribute("onclick", "SubmissionInfo()"); //what can I replace onclick with?
    position.parentNode.insertBefore(element, position.nextSibling);
}

I want this function to run with the context of which image was clicked by using the contents of the name variable, as well as the x,y coords of the images

function SubmissionInfo() {
 ...
}

I know SubmissionInfo() should be globally accessible in its own .js file, but I don't know how to get it to detect the image being clicked and how to get the id of the image.

Also, I've only been working with both html and javascript for a few hours now, so I apologize for any major mistakes I might have made.

3 Answers 3

0

You can pass the id of the element that triggers the onclick using this.id

element.setAttribute("onclick", "SubmissionInfo(this.id)");

Change the SubmissionInfo() function accordingly

function SubmissionInfo(id) {
    ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

is this possible in a chrome extension? Because that is what I am using this for, and I think I read somewhere that onclick is no longer allowed. Apologies, probably should have mentioned that.
onclick works on all browsers. stackoverflow.com/questions/3526478/…
0

Inside the below function you can get id of clicked image by

function SubmissionInfo(){
    alert(this.id);  //this would refered the current clicked image.

}, 

And for your comment, You can use the element.addEventListener('click', SubmissionInfo) instead of element.setAttribute("onclick", "SubmissionInfo()");

Comments

0

You can try using this -

element.addEventListener('click', 'SubmissionInfo(event)');

function SubmissionInfo(event){
    do_my_stuff_with_id(event.currentTarget.id);
}

And you are right onclick does not work on chrome ext.

1 Comment

I tried this, but it seems the 'SubmissionInfo(event)' is not working. it only does anything when the two '' are removed. However, when they are, chrome keeps complaining about SubmissionInfo not being defined.

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.