Change Textbox BackGround from Javascript

2

Any way to change the background color of a asp:Textbox with the event OnClientClick of a asp:Button from JavaScript unused:

document.getElementById("un_textbox").style.background = '#f88067';

The button is defined as follows:

 <asp:Button ID="re" runat="server" autopostback="True" OnClick="re_Click"  OnClientClick="return comprueba();" style="height: 32px" Text="Registrate" />
    
asked by Necroyeti 27.07.2017 в 16:31
source

3 answers

3

The only way I could achieve it is by declaring a variable js, assigning it the id of div and then passing it as a parameter the variable to the function:

<script>
   var id = "<%= txtBuscar.ClientID  %>";
</script>
<asp:Button OnClientClick='return comprueba(id)' runat="server" Text="Comprobar"/>

And in your js file:

   function comprueba(divId)
   {
      docuement.getElementById(divId).style.background = "red";
      return false;
   }
    
answered by 27.07.2017 / 17:03
source
0

I recommend that you stop denying with pure javascript and install some libraries like JQuery in the alternative and not so cumbersome way.

Usually ASPX elements when drawn in HTML end with the ID changed

<asp:Button ID="re" runat="server" autopostback="True" OnClick="re_Click"  OnClientClick="return comprueba();" style="height: 32px" Text="Registrate" />

so a call for example to instantiate that button via id, placing it as it was named would not work:

$("#re")

but instead you can use a regular pseudo expression to matchearlo to work.

$("[id$='re']")

said that given a textbox let's say the following, if it is an html component, either of the two forms will work if it is an asp component, use the second to reference, and to change the background color it would be:

 $("#elementId").css('background-color', "#f00000")

Finally an example using jquery:

function cambiarColor(elementId) {
  $('#' + elementId).css('background-color', '#F00');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="textbox">
<button type="button" onclick="cambiarColor('textbox')">cambiar color</button>
    
answered by 27.07.2017 в 20:23
-1

you must access the id of your div

function changeColor() {
  document.getElementById("un_textbox").style.backgroundColor = 'RED';
}

changeColor();
<div id="un_textbox">hola mundo</div>
    
answered by 27.07.2017 в 16:37