admin管理员组文章数量:1391969
I have tried a few solutions I have seen and none of them are actually changing the background color of my text box. These are the 2 things I have tried
1) state.style.backgroundColor = "#FF0000"
2) state.style.backgroundColor = "red";
And this is the function I am trying to use
<script type="text/javascript">
function VerifyStateWasEntered() {
var state;
state = document.getElementByID("txtstate").value;
if (state == '') {
alert("Error Please Enter A Valid State");
state.style.backgroundColor = "red";
return false;
}}
And this is the HTML I am using to create the text box
<asp:TextBox id="txtstate" runat="server" />
I have tried a few solutions I have seen and none of them are actually changing the background color of my text box. These are the 2 things I have tried
1) state.style.backgroundColor = "#FF0000"
2) state.style.backgroundColor = "red";
And this is the function I am trying to use
<script type="text/javascript">
function VerifyStateWasEntered() {
var state;
state = document.getElementByID("txtstate").value;
if (state == '') {
alert("Error Please Enter A Valid State");
state.style.backgroundColor = "red";
return false;
}}
And this is the HTML I am using to create the text box
<asp:TextBox id="txtstate" runat="server" />
Share
Improve this question
edited Nov 5, 2015 at 22:53
11mb
1,3592 gold badges17 silver badges33 bronze badges
asked Oct 18, 2015 at 20:34
MustangLoverMustangLover
351 gold badge1 silver badge6 bronze badges
1
- Since you use asp control you may run into a problem with .NET generating the id. In this case you get a pound of the control nestings ("MainContent_Login1_LoginPanel" say). Check the ClientIDMode setting in the Page directive and set the value to "static" to avoid this. – faester Commented Oct 18, 2015 at 20:38
3 Answers
Reset to default 3Can you try this? When you assign the state to the value, you cannot get the style object from value.
<script type="text/javascript">
function VerifyStateWasEntered() {
var stateObj;
stateObj = document.getElementByID("txtstate");
if (stateObj) {
var stateValue = stateObj.value;
if (stateValue == '') {
alert("Error Please Enter A Valid State");
stateObj.style.backgroundColor = "red";
return false;
}}
}
</script>
<asp:TextBox id="txtstate" runat="server" ClientIDMode="Static" />
What you are attempting is possible, but I suspect you use the wrong ID (as per my ment). To set the color of an element you can use the JavaScript below.
document.getElementById("txtstate").style.background = "red"
But you need to make sure you have ClientIdMode="static"
on the page.
I get that you are trying to make the background color using Javascript but you can use CSS to do that and it will be much more easier.
Example:
<asp:TextBox id="txtstate" runat="server" />
<style type="text/css">
#txtstate {
background-color: /* the color you want */ ;
}
If that still doesn't work, try removing the closing /
from <style type="text/css">
. Good luck!
本文标签: htmlChange Text Box Background Color Using JavaScriptStack Overflow
版权声明:本文标题:html - Change Text Box Background Color Using JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744757273a2623539.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论