7
Hello, when clicking one of the 3 options in "HTML" that is in the code below, I want the function in "JS" emits an "Alert" with the "idPost" in which it was clicked. EX: When I click on the "PHP" link it emits an Alert 25, which would be its idPost. But when I click on the other options the function does not emit any Alert. If I did two other functions and changed the ids, I know it would work, but as it is a posting system, that would be my options, when adding a new post I would have to do another new function, so I used the same ids. I hope that’s clear!
HTML code:
<p id="linkPost" idPost="25"><a href="#">PHP</a></p>
<p id="linkPost" idPost="26"><a href="#">HTML</a></p>
<p id="linkPost" idPost="27"><a href="#">JAVASCRIPT</a></p>
JS Code:
$('#linkPost').on('click', function(){
var id = $(this).attr('idPost');
alert(id);
});
Do you click PHP? This doesn’t make much sense, you mean that in a php document js works, but in an HTML document it doesn’t?
– Guilherme Nascimento
Like, this is supposed to be a posting system, this is just a test, could be other titles. When I click on one of these titles of the same ID I wanted the function to take idPost, which I will later use in other things!
– José Luis
Still not making sense to click on PHP.
– Guilherme Nascimento
PHP would be the subject of a post, clicking it would open the post. PHP is just a test
– José Luis
No matter the content of your PHP, what makes no sense is what you asked, in other words we can not understand what you mean by "click on PHP", for you can even make sense, but remember just because you understand your own problem doesn’t mean you can pass it on to other people... Then edit the question and make a clearer text please. Grateful.
– Guilherme Nascimento
the answers of Rafel and Sérgio solve the problem, however, there is a bigger problem that is the semantics.
– Daniel Omine