Jump to content

Setting A String Attribute To An Element


sairfan1
 Share

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.

Edited by Deirdre's Dad
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...