<p>some text</p>
<p>some text</p>
<p>some text</p>
<p>some text</p>
<p>some text</p>
<p>some text</p>
<p>some text</p>
Is there any way to identify that when a mouse is clicked at random.
Is there anyway to get nth element of the selected through mouse ?
edit: when we click over a paragraph, i am using jquery
<p>some text</p>
<p>some text</p>
<p>some text</p>
<p>some text</p>
<p>some text</p>
<p>some text</p>
<p>some text</p>
Is there any way to identify that when a mouse is clicked at random.
Is there anyway to get nth element of the selected through mouse ?
edit: when we click over a paragraph, i am using jquery
n
in this case? How is the nth element related to the clicked element?
– Felix Kling
Commented
Nov 25, 2012 at 10:41
This logs the index of the paragraph that was clicked.
var $elems = $('p');
$elems.on('click', function(e) {
var indexOfElem = $elems.index(this);
console.log("Element with index: " + indexOfElem + " was clicked.");
});
Something like this?
The jQuery index
function in jQuery returns the position of an element within the jQuery object. To find the position of the clicked element within some list:
var $elems = $("#context > p");
$elems.on("click", function() {
var i = $elems.index(this);
console.log(i); // use the index
});
try this :
$('p').click(function () {
alert($('p').index(this));
});