javascript - addClass("test") give error: TypeError: undefined is not a function -


in console have:

$(".mycssclass")[0].parentnode <li><span class="mycssclass">some text</span></li> 

i want add css class parent span, tag <li>

i tried this:

$(".mycssclass")[0].parentnode.addclass("test") typeerror: undefined not function 

i use jquery 1.5.2

addclass method of jquery objects. when use $(".mycssclass")[0], have real element, not jquery wrapper.

then, can:

  • wrap jquery object again:

    $($(".mycssclass")[0].parentnode).addclass("test") 
  • only work jquery objects:

    $(".mycssclass").eq(0).parent().addclass("test") 
  • add class plain javascript (not supported on old browsers):

    $(".mycssclass")[0].parentnode.classlist.add("test") 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -