1

In a dinamically generated "ol" :

document.getElementsByTagName('ol');
    for (i = 0; i < len; i++){
          var newLi = document.createElement("li");
          var link = document.createElement('a');
          link.href = "#"; 
          link.innerHTML = (results.rows.item(i).location + "-" + results.rows.item(i).datte);
          newLi.appendChild(link);
          olnew[0].appendChild(newLi);

i need find the "li" clicked, i use jquery library only for this function, i am searching for the same functionality in javascript, but at momento i have not idea how i can code it. thx var ss; ss=$("#idfromOl"); ss.click(clickhecho);

}

function clickhecho()
{
    var $all_lis = $('li');

    $all_lis.on('click', function() {
        var index = $all_lis.index(this);
    });
}

4 Answers 4

1

try this:

function createfunc(i) {
    return function() { alert(i); };
}
for (i = 0; i < len; i++){
    var newLi = document.createElement("li");
    var link = document.createElement('a');
    link.href = "#"; 
    link.innerHTML ="test"
    newLi.appendChild(link);
    // just add onclick event; use createFunc to create function closure (otherwise 'i' would always be the last 'i'
    newLi.onclick = createfunc(i);

    olnew[0].appendChild(newLi);
}
Sign up to request clarification or add additional context in comments.

1 Comment

it work fine, thx. not sense use a jquery library for 4 lines of javascript
0

I might be mistaken, but is this what you are looking for?

var elements = document.getElementsByTagName('li');

for (var i = 0; i < elements.length; i++) {
    elements[i].addEventListener('click', function (e) {
        $("#clickedLi").text(e.srcElement.id);
    });
}

It appends an event called addEventListener to every li element.

Inside each li element there is an click event given as a parameter which contains the id of the li clicked.

demo: http://jsfiddle.net/DUzMc/1/

2 Comments

It seems user2764662 is trying to avoid jquery.
It's not jquery, exept for the text() function, but thats not what he'll use, his question was how to determine which li was clicked using nothing but javascript. The text() is only used to display the id into an textbox which is created into the example.
0

Your first part of script is incomplete, so I did a short script to generate a dynamic list.

Basicly you were looking for addEventListener():

var elements = 10;
ol = document.createElement('ol');
for(i = 1; i <= elements; i++){
    var li = document.createElement('li');
    var a = document.createElement('a');
    a.setAttribute('href', '#');
    a.text = 'Link ' + i;
    li.appendChild(a);
    ol.appendChild(li);

    a.addEventListener("click", who, false); // THIS IS THE IMPORTANT PART
}

document.getElementsByTagName('body')[0].appendChild(ol);


function who(e){
    var myTarget = e.target;
    myTarget.text = "clicked!";
}

Comments

0

The easiest way is to include the event while generating the list:

document.getElementsByTagName('ol');
for (i = 0; i < len; i++){
      var newLi = document.createElement("li");
      var link = document.createElement('a');
      link.href = "#"; 
      link.innerHTML = (results.rows.item(i).location + "-" + results.rows.item(i).datte);

      // Add this
      link.onclick = function(index) { return function() {
        // do something with index variable
      }}(i);

      newLi.appendChild(link);
      olnew[0].appendChild(newLi);

Note I use a local variable index instead of i, this is because when the item is clicked the value of i will be different (equal to len).

1 Comment

Will not work because of closure issue: 'i' would always be the same (last loop value).

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.