JQuery Function Not Updating Hidden Field - javascript

I have two hidden fields for business and home address. They are updated to "true" on the event that the content for the specific textboxes are changed. However, because there are various fields within the div, I used jquery to get all objects of type text within the div. Basically what I'm trying to do is check if the text has changed and then update a hidden field value on my ascx form to true.
InitAddressOnChange: function () {
$('#divBusinessAddress input[type=text]').change(function () {
$("[id$='hiddenBusinessField']").val("true");
});
$('#divHomeAddress input[type=text]').change(function () {
$("[id$='hiddenBusinessField']").val("true");
});
}
Here are my hidden fields:
<asp:HiddenField ID ="hiddenBusinessField" runat="server" value="false"/>
<asp:HiddenField ID ="hiddenHomeField" runat="server" value="false"/>
I tried debugging in the browser but I'm having an issue figuring this out. Is there anything I'm doing wrong here? I'm fairly new to JQuery.

Related

dropdownlist not changed SelectedValue on server side

I have an dropdownlist
<asp:DropDownList ID="ddlNationality" runat="server" ClientIDMode="Static">
</asp:DropDownList>
in document ready I set
ddlNationality.selectedIndex = -1;
for show to user nothing selected
After I change value and click some button on server side my selected value is alway zero, after I change selected value in javascript I check if selected values is changed and all i ok, but o server side selected value not changed,.. how to proceed?
I can to insert in this dropdownlist first item with no value and no text, but I want to have in dropdownlist only my values without clear field
looks like you are missing attribute AutoPostBack="true"
When you are not posting back and changing the value of dropdownlist in client, asp.net cannot be informed about it.
Create a hidden field:
<input id="MyHiddenField" type="hidden" runat="server" class="my-hidden-field" value="" />
Whenever you are changing the value in client set the value to hidden field:
$(document).ready(function () {
$('select.my-ddl-class').on('change', function () {
$('.my-hidden-field').val(this.options[this.selectedIndex].value);
});
});
Then, on the code-behind instead of getting the DropDownList selected value, read hidden field value:
selectedValue = int.Parse(MyHiddenField.Value);

Show hidden asp.net control via jquery or javascript

I have a hidden control (an asp.net label) and want to show it when a user clicks a button.
Here is the hidden control
<asp:Label ID="lblCityRequired" visible="False" runat="server" Text="Required if Country State are selected"></asp:Label>
Here is the javascript code
function showLabel()
{
$("#lblCityRequired").show();
}
function hideLabel()
{
$("#lblCityRequired").hide();
}
This only works when the label's visibility is not set to false. However I want the form to start up hiding the message. Is there something I am doing wrong? Should I just create a javascript function that starts up hiding the label via javascript?
Thanks in advance
You can hide it initally when dom is ready:
$(function(){
hideLabel();
})
Or, you can use css:
<style>#lblCityRequired{display:none;}</style>

Hidden field value not accessible from javascript

I have hidden field,
<asp:HiddenField ID="HiddenField1" runat="server" Value="" ViewStateMode="Enabled" />
I am setting value of hidden field in code behind file in videoFile_UploadedComplete event (AsyncFileUpload control's event)
HiddenField1.Value = "yespost";
I am trying to access value in javascript,
if (document.getElementById("<%=HiddenField1.ClientID%>").value == "yespost")
Its giving empty string ("").
I am calling script on OnClientUploadComplete event.
Note: First server side event gets executed and client side so value is set first.
Help needed
You want to select by id. Add a '#' before your selector:
if (document.getElementById("#<%=HiddenField1.ClientID%>").value == "yespost")

How to get the values from jQuery and assign it for hidden field and save it into the DB?

How to get the values from jQuery and assign it for hidden field and save it into the DB?
Actually I am developing a project, where there is some jQuery code for a button:
$(document).ready(function () {
$("#btnYes").click(function () {
$("#btnYes").html("Counted");
$("#<%= hdnYesNoAnswer.ClientID %>").val("Yes");
$('#txtComment').focus();
});
});
When I clicked the #btnYes, it changes to "Clicked". In that page I added one hidden field called #hdnYesNoAnswer. So now what I want is to get the value ("Yes") from that jQuery function and assign it to hidden field #hdnYesNoAnswer and save that hidden field value to the SQL Server database.
#Maris is right. Ajax is a defacto technic for these actions. Here are some links. I hope they help
http://api.jquery.com/jquery.ajax/
http://en.wikipedia.org/wiki/Ajax_(programming)
https://developer.mozilla.org/en/docs/AJAX

Using JQuery onblur to set textbox value

I have a textbox with these rules:
1) I populate the textbox.text from a dictionary in session
2) If the user enters a new value, setTextBoxData will save it in the dictionary
3) On entry (on focus) the field text is blanked.
4) On blur, if the field is still empty, I want to set it to the original value.
<asp:TextBox ID="txtNumberEmployees" runat="server" Width="50px" onfocus="this.value='';"
onchange= "javaScript:$(function()setTextBoxData('NUMBEREMPLOYEES','txtNumberEmployees');});"
onblur="javaScript:restore ('txtNumberEmployees', 'NUMBEREMPLOYEES');"/>
The "restore" function referenced above is:
function restore(control, input) {
var data = getInputData(input);
$('#' + control).val(data);
}
getInputData returns the data value correctly. The problem is with the last line.
I have tried many ways to set this, but none seem to work. It should be a simple problem, but I can't get it to work yet.
The problem is ASP.NET will generate an ID that will not be txtNumberEmployees. ASP.NET will generate an ID for your input that will end with txtNumEmployees.
Change this line:
$('#' + control).val(data);
to this:
$('[id$=' + control + ']').val(data);
It will work because this is the Attribute Ends with Selector.
1: Make sure you have no javascript errors. I see there's a missing '{' in the onchange.
2: You can simply pass 'this' as the textbox reference and update it like below:
<asp:TextBox ID="txtNumberEmployees" runat="server" Width="50px" onfocus="this.value='';"
onchange= "javaScript:$(function(){setTextBoxData('NUMBEREMPLOYEES',this);});"
onblur="javaScript:restore (this, 'NUMBEREMPLOYEES');"/>
then simply set the value like:
$(control).val(data);
3: There are other ways as well to grab an asp.net element like shown here. Find ASP.NET ClientID in jquery

Categories

Resources