Currently I get a count of rows through a small query and this result is sent to my main page that is created in asp.net
I am currently doing it in these ways and none have worked for me.
form 1
<script type="text/javascript">
$(document).ready(function () {
var lbltext = document.getElementById('lblPlanta').innerHTML;
alert(lbltext);
});
</script>
form 2
<script type="text/javascript">
$(document).ready(function () {
var lbltext = document.getElementById('lblPlanta').textContent;
alert(lbltext);
});
</script>
form 3
<script type="text/javascript">
$(document).ready(function () {
value = $("#lblPlanta").text();
alert(value);
});
</script>
form 4
<script type="text/javascript">
$(document).ready(function () {
value = $("#lblPlanta").val();
alert(value);
});
</script>
form 5
<script type="text/javascript">
$(document).ready(function () {
var lbltext = document.getElementById('lblPlanta').value;
alert(lbltext);
});
</script>
This is how the Label is created. (In the code the label is above the script)
<label id="lblPlanta"></label>
Initially, the label is empty because it is filled in automatically, with the result that it returns from the DB. I print said result in that label and it is printed, but the problem is that I cannot obtain it to assign it to a variable.
Assignment of information to the label
function getPlantas() {
block();
$.ajax({
url: "../../../pagina/configuracion/empresa/confEmpresa.aspx/getPlanta",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
$("#lblPlanta").html(data.d);
$.unblockUI();
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
$.unblockUI();
}
});
}
Update
Apparently my problem is that I try to get a value before it is loaded through document.ready, so I replaced it with the following method:
$(window).load(function () {
value = $("#lblPlanta").text();
alert(value);
});
But the alert keeps coming up blank, could someone tell me a way to make my method run after everything has been loaded.
AJAX calls are not deterministic in how long it may take to get the result of the request. Most of the time it will be returned almost instantly, but it is not possible to control that, the server that attends to the request may be congested and take longer than normal to respond and you cannot know that in advance.
That's why the way is not to subscribe to window.onload or dom.ready , since the code that makes the AJAX call will most likely start executing precisely in response to one of these two events, and therefore it will still be some time since the above events are fired and get the response from the server and put the result in the #lblPlant element .
A correct solution has already been proposed by @Naos and it is to subscribe to a custom event on the #lblPlanta element that reads its value, and launch this event from the end of the success method that makes the AJAX request.
I hope this clarifies the problem a bit.
Good luck and Greetings!
Based on your question and the comments you have made and other users have commented you can listen on your document when the content of
x elemento
is changed, example:So to make it work in your code you can simply add this before your function and a line inside it like so:
You can do it with the same function with which you assign:
But you can't use it in a
since that code is executed when loading the page and at that moment you still don't have the value loaded. You will have to get it when you need it.
Declare a global variable
Assign it in the getPlants function
Once you've done that you can use the plant variable and get its value
It's designed to do (among other things) is to make sure that your function is called once all the DOM elements on the page are ready to be used.
I think that would be it.