Jump to content

Setting A String Attribute To An Element


sairfan1

Recommended Posts

hi, im try to set a string type attribute of an element like thisMenuItem.setAttribute('onmouseover','this.className=myclass')but in result it removes quote signs and result is onmouseover=this.classNamei also tried 'this.className=myclass' and '"this.className=myclass"' and '"this.className=myclass"'but failedi want out put like this onmouseover='this.className=abc'please help thanks

Link to comment
Share on other sites

We do not add event listeners with setAttribute. This page at Mozilla explains the correct DOM techniques, which must be adapted to be cross-browser. The traditional technique is also shown. The traditional way remains my preference because it is simple and always works. Here is more discussion of that.

Link to comment
Share on other sites

Hi thx for reply.im receiving object does not support this property/method error while using .addEventListenermore over this code soved the problem .setAttribute('onclick','this.className="ABC"')

We do not add event listeners with setAttribute. This page at Mozilla explains the correct DOM techniques, which must be adapted to be cross-browser. The traditional technique is also shown. The traditional way remains my preference because it is simple and always works. Here is more discussion of that.
Link to comment
Share on other sites

Hi thx for reply.im receiving object does not support this property/method error while using .addEventListenermore over this code soved the problem .setAttribute('onclick','this.className="ABC"')
(Re?)read the Internet Explorer part of the article above. If the drawback is troublesome for you, use the older way to register event listeners.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...