Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - if text in DIV was clicked

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;
}
like image 740
Patrik Avatar asked Feb 18 '13 12:02

Patrik


1 Answers

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");
});
like image 115
Adil Avatar answered Sep 28 '22 09:09

Adil