0

I have the following:

<div onclick="updateElementWithValue()" id="element">Value 1</div>
<div onclick="updateElementWithValue()" id="element">Value 2</div>
<div onclick="updateElementWithValue()" id="element">Value 3</div>

JS:

updateElementWithValue(){
//need to add some code here to make the function work!
alert('This is element : ');
}

what I want to do, is for example if I click on :

<div onclick="updateElementWithValue()" id="element">Value 2</div>

It displays the value of that div, in the above case, it should show : Value 2

Any ideas?

2
  • 1
    Don't use the same ID for multiply elements: id="element" Commented May 8, 2016 at 13:25
  • @andlrc I have a for loop that prints the same element ID Commented May 8, 2016 at 13:26

1 Answer 1

1

First: don't use the same id for multiple elements.

Second: give the element as parameter: updateElementWidthValue(this).

The function is:

function updateElementWithValue(element) { 
    alert('This is element: ' + element.innerHTML);
}
Sign up to request clarification or add additional context in comments.

2 Comments

You might tell a little about node.addEventListener('click', ...);
this will always get value1 because he is putting the same id for the DOM element

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.