I have some DIVs. Inside these DIVs are some texts, images, tags... I need to show "alert", when text is clicked. Not images, not other tags, not text in other tags... just text inside DIVs. How to do it with jQuery ?
jsFiddle example.
HTML
<div style="background-color:PapayaWhip;">Some text.
<img src="http://ynternet.sk/test2/close_1.jpg" />
<img src="http://ynternet.sk/test2/close_2.jpg" />
</div>
<div style="background-color:Moccasin;">This are letters
<em>and this are not.</em>
Hello again!
</div>
<div style="background-color:LightGoldenRodYellow;">
<img src="http://ynternet.sk/test2/close_1.jpg" />
<img src="http://ynternet.sk/test2/close_2.jpg" />
Letters in new line.
<img src="http://ynternet.sk/test2/close_1.jpg" />
<img src="http://ynternet.sk/test2/close_2.jpg" />
</div>
<div style="background-color:Wheat;">New letters in another line.</div>
CSS
div {
padding:10px 0px 0px 20px;
font-family:Verdana;
height:40px;
}
em{
color:red;
}
Check the type of source if div then it means it would be text wrapped directly by the div. If you want to include mode tag like em
then you can add those in condition.
Live Demo
$('div').click(function(evt){
if(evt.target.tagName == 'DIV')
alert("div clicked");
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With