Problems hiding/displaying div with Javascript

Asked

Viewed 820 times

5

I have a relatively simple problem, but I cannot find a quick solution: I have a dropdownlist which determines which div will be displayed, and the moment the page is loaded for the first time, it works perfectly. Soon after saving the data (ie give refresh on the page), the option that was hidden before that refresh is no longer displayed when selected in dropdownlist .

The function below is called when selecting the dropdownlist . Just below, the dropdownlist where I call the function.

NOTE: I tried using jQuery, I tried calling the event on Postback page (and outside of it too), but the impression is that my div gets lost at some point.

I’ve also tried putting the display:none/block right in the div, without success. Both Divs are fixed in the .aspx, and are not made dynamically.

OBS. 2: Inside the divParcelaVariavel, there are two UpdatePanel, used on two buttons.

function MudarEstado() {

     var fixas = document.getElementById('ContentPlaceHolder2_divParcelaFixas');
     var variaveis = document.getElementById('ContentPlaceHolder2_divParcelaVariavel');


     if (document.getElementById('ContentPlaceHolder2_dropTipoParcelas').value == 'V') {
         variaveis.style.display = 'block'
         fixas.style.display = 'none'
        }
     else {
         variaveis.style.display = 'none'
         fixas.style.display = 'block'
        }
    }

  • I don’t quite understand your question, but use the browser console to check the style of the div.

  • Basically it is the following: I have two Ivs, "Fixed Parcel" and "Variable Parcel". If, for example, I select the Variable first and save, after this postback the Fixed div is not rendered on the screen when selected in my drop. In the source code of the page it appears, but on the screen, no. And I saw by debug that this element (Document.getElementById('Contentplaceholder2_divparcelafixas') returns null.

  • Your Mudarestado function is inside a $(Document). ready(Function(){ }); ? When you inspect the page you find this div??

  • No it is not, but I find the div yes. It is there but it is not displayed.

  • The funny thing is this null return, even if it is loaded internally...

  • Put on the console Document.getElementById('Contentplaceholder2_divparcelafixas') to see if it returns null. By the way the name is Contentplaceholder2_divparcelafixas or Contentplaceholder2_divparcelafixa?

  • Returned null. The name is Contentplaceholder2_divparcelafixas. Typing Document.getElementById('Contentplaceholder2_divparcelavariable') it brought <div id="Contentplaceholder2_divparcelavariable" class="caixa2" style="display: None;">.

  • <div id="Contentplaceholder2_divparcelafixas" class="caixa2"> <fieldset> <Legend><b>Fixed parcel</b></Legend> <table> <tr> <td class="style9">Number of Parcels</td> <td> <input name="ctl00$Contentplaceholder2$textNumeroParcelas" type="text" id="Contentplaceholder2_textnumero parcelas" onKeyPress="javascript:fieldNumerico(this);" style="width:50px;" /> </td> "Display source code", proving that it is there.

  • It returns null, but inspecting you find... How strange.. I thought I might have misspelled the id...

  • It may be that you are trying to locate the div before it exists. Or there is even some typo as you imagined @Joaopaulo.

  • @bfavaretto I imagined that she tried to locate before it existed, but she said that it is not dynamic and even running the code on the console with the div already there returned null.

  • What I thought happened is this: The moment the page loads, I for example choose the div Variables and hidden the Fixed. When saved and return to the screen, he understands that the Fixed div still needs to be hidden and shows only the variables. Is there any way I can "zero" that state of the Divs?

Show 7 more comments

1 answer

2


UpdatePanel is an update of a piece of the screen. If only a piece of the screen is updated you end up losing your Javascript/Jquery, losing the function that your Dropdown performs, take a read here to better understand. Use the code below for a new loading of the Javascript function after a block update.

 var prm = Sys.WebForms.PageRequestManager.getInstance();
     prm.add_endRequest(function () {
       $("#dpw").click(function () {     
        mudarEstado();
      });
    });

And matenha Here.

$(function () {
 mudarEstado();
});

Your Function Javascript should look like this. I decided to put the select and option because in the end Dropdown and Listitem do not become the same for the browser.

Here is an example of how it should be done..

 <form id="form1" runat="server">
        <div>
            <select id="dpw" onclick="mudarEstado()">
                <option selected="selected" value="S">Ocultar</option>
                <option value="N">Mostrar</option>
            </select>
            <br />
            <div id="oculta" style="display:none;background-color: #000; width: 30px; height: 40px"></div>
        </div>
    </form>
    <script type="text/javascript">
        function mudarEstado() {
            var value = document.getElementById("dpw").value;
            if (value === "S")
                document.getElementById("oculta").style.display = "none";
            else
                document.getElementById("oculta").style.display = "block";
        }

    </script>

  • None of the options worked... it calls the function but goes back to the page without displaying the div =/ Thank you so much for the help!

  • I will post the relevant sections, the page is very extensive. It is worth noting: There is a connection time counter on this page, also done in Javascript. Dropdownlist Declaration: <td class="style9">Parcels </td><td><Asp:Dropdownlist ID="dropTipoParcelas"runat="server"onchange="javascript:Mudarestado();"><Asp:Listitem Value="></Asp:Listitem> <Asp:Listitem Value="F" Text="Fixed"></Asp:Listitem value><Asp:Listitem Listitem="V" Text="Variables"></Asp:Listitem></Asp:Dropdownlist>

  • I saw yes, I’ve used those examples on other screens of the system but in this case did not help, unfortunately.

  • Changestate: Function Mudarstate() { var fixed = Document.getElementById('Contentplaceholder2_divparcelafixas'); var variables =Document.getElementById('Contentplaceholder2_divparcelavariable'); if Document.getElementById('Contentplaceholder2_droptipoparcelas'). value == 'V') {variables.style.display = 'block' fixed.style.display = 'None'}Else { variables.style.display = 'None' fixed.style.display = 'block' } }

  • I tried a call to this function in the Postback page, also without success: Function Estadoload() { Alert("entered"); Document.getElementById('Contentplaceholder2_divparcelavariable').style.display = 'block'; Document.getElementById('Contentplaceholder2_divparcelafixas').style.display = 'block'; }

  • On page Postback: Scriptmanager.Registerstartupscript(this.Page, typeof(Page), "Mudarestado", "Estadoload();", true);

  • No reputation enough for chat

  • @Arethafreitas Let’s delete the comments?

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.