1

I have this line of JavaScript / jQuery that I'm attempting to use to append an element to the DOM and then attach an eventlistener to.

$('.faq_answer').prev().append(finalRender.cloneNode(true)).addEventListener("click", function () {toggle(this)}, false); 

I know the appending part works perfectly but adding an event listener is giving me grief.

Is it possible / advisable to use jQuery and normal JavaScript together like this? Or is there something in jQuery that would work better. (Very new to jQuery so bear with me).

10
  • jQuery and Javascript are incompatible. jQuery can't take it... (I'm just kidding) Commented Jul 4, 2011 at 15:10
  • if you are creating dynamic elements, use api.jquery.com/delegate to apply the event listener Commented Jul 4, 2011 at 15:11
  • To which node should the click handler be bound? Commented Jul 4, 2011 at 15:12
  • @Felix - I'm assuming he's attempting to bind it to the inserted element, hence my relocation of the listener in my response. Commented Jul 4, 2011 at 15:13
  • @all Sorry, yes I intend to bind to the inserted element Commented Jul 4, 2011 at 15:17

5 Answers 5

2

If you want to use the native methods, then you need to call them against DOM elements, not jQuery objects

var clone = finalRender.cloneNode(true);
clone.addEventListener("click", function () {toggle(this)}, false);

$('.faq_answer')
      .prev()
      .append( clone );

If you want to use jQuery, then you need to wrap the DOM elements in a jQuery object.

  // Drop your DOM element----v----into a jQuery object
var clone = $( finalRender.cloneNode(true) ).bind("click", function (){
    toggle(this);
});

$('.faq_answer')
      .prev()
      .append( clone );
Sign up to request clarification or add additional context in comments.

Comments

0

You could do something like this:

 $('.faq_answer').prev().append(finalRender.cloneNode(true)).click(function () {toggle(this)});

which is pure jquery

Comments

0

I think you're wanting to add your event Listener to the cloned Node - not to the prev() of .faq_answer.

If you're importing jQuery, why not use the .click() method anyway instead of mixing? It's simpler! :)

$('.faq_answer').prev().append(finalRender.cloneNode(true).click(function(){
    toggle(this)
}));

2 Comments

You're trying to call a jQuery method against a DOM element. That won't work.
What if you wrap it in jquery goodness like $(finalRender.cloneNode(true)).click(... etc ?
0

Yes, it's possible to use them "together" because jQuery is JavaScript. However in this case there's really no reason to bind event handlers directly like that if you are using the library. It already knows how to manage event handlers, and doing it outside of its control is probably not a good idea unless you really know what you're doing.

Comments

0

I find questions like this are strange, because jQuery is JavaScript.

Based on this mixing up the code that is just plain JavaScript with jQuery code is fine.

But if there is a simpler way of doing what you want in jQuery then I would say to just use jQuery alone.

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.