开发者

How can i add attribute to a textbox using javascript?

开发者 https://www.devze.com 2023-03-15 07:39 出处:网络
i ha开发者_如何学Cve a datalist in which i have a text box name txtvoteoption now on i add attributes on item data-bound on this text box but now i want to add the third attribute using java script ?

i ha开发者_如何学Cve a datalist in which i have a text box name txtvoteoption now on i add attributes on item data-bound on this text box but now i want to add the third attribute using java script ?

the reason behind i want this on java script because i have a drop down on this page also which change using drop down and i want to add this attribute on this change event

this code which is working fine on item data-bound event

txtVoteOption.Attributes.Add("onfocus", "EnableTip('" + txtVoteOption.ClientID + "','text',1);");
            txtVoteOption.Attributes.Add("onblur", "DisableTip('" + txtVoteOption.ClientID + "','text');");
            txtVoteOption.Attributes.Add("validation", "Required,Please enter option A,default.png;4");

i tried but its not working this is javascript code.

   var txtOption1=document.getElementById("ctl00_cphContent_dlVoteOption_ctl01_txtVoteOption");
  alert(txtOption1);
  txtOption1.attributes.add('validation', 'Required,Please enter option A,default.png;4');


It the first example it seems you use some kind of framework. In the second you don't.

txtOption1.attributes returns a NamedNodeMap [docs]. It does not have a method add.

You have to use element.setAttribute [docs] and then access it with element.getAttribute [docs].

You should also consider using HTML5 data attributes for compatibility. Avoid adding any other kind of self defined attributes. Every element has a well defined set of allowed attributes.

0

精彩评论

暂无评论...
验证码 换一张
取 消