0

I want to convert this jquery to javascript.Please help

$("tr").live('click', function() {

alert("row number: " + $(this).index());

});
11
  • download jquery library (unminified) and copy the live function (note that live disappeared in 1.7+, so download at max 1.7) Commented Apr 2, 2014 at 10:33
  • why you want to use pure javascript? The snippet you given evn if it uses jquery is still javascript Commented Apr 2, 2014 at 10:33
  • Maybe because that's all they need and using jQuery is an unnecessary load. Also vanilla JS is always faster. @Ramesh Commented Apr 2, 2014 at 10:34
  • 1
    Do you have any skills in native js? Because the jQuery selector isn't that easy to reproduce. Did you had a look at the jquery function? Commented Apr 2, 2014 at 10:35
  • 1
    @pc-shooter — It's trivial to reproduce so long as you have a modern browser. querySelectorAll. Commented Apr 2, 2014 at 10:36

2 Answers 2

2

See this:

var elemm=document.getElementsByTagName("tr");
var cnt = 0;
for(var i = 0;i<elemm.length;i++)
{
    elemm[i].onclick = function(){alert("row number: " + ++cnt)}
}

Working Demo

Sign up to request clarification or add additional context in comments.

Comments

1

Fiddle Demo

js

function indexInParent(node) {
    var children = node.parentNode.childNodes;
    var num = 0;
    for (var i = 0; i < children.length; i++) {
        if (children[i] == node) return num;
        if (children[i].nodeType == 1) num++;
    }
    return -1;
}

function test(el) {
    alert('row number: ' + indexInParent(el));
}

in html add

<tr onclick="test(this)">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.