0
Imagine the following code below:
<a id="link" href="/nova_pagina.php">
<div id="abrir-nova-pagina" >Linha_1</div>
<div id="nao-abrir-nova-pagina" >Linha_2</div>
<a/>
I want the a#link element to work when I click on the first div and open a new page and I don’t want the link to work when I click on the second div.
How can I do that?
I saw some examples with jQuery using the stopPropagation() method but it didn’t work.
Look below:
$('a#link').on('click', function(event){
event.stopPropagation();
});
$('#nao-abrir-nova-pagina').on('click', function(event){
alert('Funcionou!')
});
What I did wrong?
Young independent of anything this HTML structure is very wrong, even if you can make it work... A link is a link, "half link" makes no sense, there’s something wrong there...
– hugocsl
Is missing the
event.preventDefault();
. You stopped the spread, but the link keeps opening. Soon, you will need to block the default action (open the link)...– LipESprY
@Lipespry did not understand
– Jose Henrique
I was going to comment precisely on what @hugocsl said. Isn’t it better just to have Linha_1 be a link? The tag function
a
is to make all its content a link. If each line behaves differently (one is link, another is not), then they should not be within the samea
– hkotsubo
<a>
 <div>Linha_1</div><a/>
<div>Linha_2</div>
– hugocsl
The
a
is also closed wrong. The correct would be</a>
and not<a/>
– Sam
I thought I’d comment on the structure of your code, as did the hugocsl. I also didn’t understand the need to "not open a link by clicking on the link"... We usually use some script associated with a link altogether. But simply answering your question, you already have an answer formulated by @Icaro Martins that matches exactly my previous comment.
– LipESprY