I have some HTML like this
<div id='div1'>some content</div>
<div id='div2'>some other content/div>
<div id='div3'>now some even different content</div>
<button onclick='CallThis(1);'>Alert 1</button>
<button onclick='CallThis(2);'>Alert 2</button>
<button onclick='CallThis(3);'>Alert 3</button>
<script>
function CallThis(SomeVar)
{
alert(document.getElementById('div' + SomeVar).innerHTML);
}
</script>
I want to use simple javascript. How can I accomplish this?
Thanks for your help.
3
Answers
Not really sure of what are you trying to accomplish, but if you would like to show some alert that varies with the clicked button, you can use if statement like below:
it really is just a basic JavaScript, maybe try to learn the fundamental first, here’s a page to learn the fundamental of JavaScript :
JavaScript Tutorial
The only thing i noticed was a typo in the second line, if we fix that, then it works.
You need to learn DOM Traversing,
or name efficiently
try use querySelectorAll("div")
more intuitive way of coding can help debug