0

I am writing a chrome extentions whihc determines the clicked elements xpath dynamically. It creates a text area web element with insertAdjacentHTML function (like following) and the clicked elements xpath is placed to it.

    document.body.insertAdjacentHTML('beforeend',this.html);
    //this.html is string version of text area web element

    document.addEventListener('click', this.getData, true);

The above code gets the xpath of the element and places it to the created text area. I also want to calculate and show the number of elements that matched with the xpath in the text area(user can edit the text area) To do that I am trying to add another eventlistener for the text area element which is like following:

 var element= document.getElementById("inspector-text-area")
          element.addEventListener('change',function(e){
            console.log("event fired")
            ...
         });

after running the extention, the xpath of the clicked element is placed to created text area web element but when I edit the text area the next added event listener's function is not fired. What is wrong I do? Thanks..

4
  • It should be "change" not "onchange" element.addEventListener('change',function(e){ Commented Dec 7, 2019 at 15:29
  • @Eldar I have corrected it, but again it does not work. By the way I also tried event type click or hover. It does not work Commented Dec 7, 2019 at 15:45
  • The code you've posted looks correct. 2 things to check: 1. is inspector-text-area actually on the page when you call getElementById("inspector-text-area")? 2. When do you expect the event to fire? The change event only fires when the field value is updated, which usually happens when the field loses focus -- not as the user is typing. If you want to see changes as the user types consider using keyup event instead. Commented Dec 7, 2019 at 16:00
  • The problem is solved. When I removed the 3tr parementer of the document.addEventListener('click', this.getData, true); it worked. By the way @ChrisCamaratta the type of the event that I want is 'keyup' event. Thank for this information Commented Dec 7, 2019 at 16:17

1 Answer 1

1

It is related with the event capturing and event bubbling

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

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.