Maintain value after Postback using jquery? - javascript

I have a select list
<select id="type" name="type" onchange="type_Change(this.id)">
and i want to maintain selected value after the postback.
how will i acheive through jquery.
please anyone???

If you change the selected value of select through javascript/jQuery code then you can store the changed selected value in some hidden field and later using it in code behind.
Html
<input type="hidden" runat="server" id="hdnForSelect" />
Javascript
function type_Change(idOfSelect)
{
//your code
document.getElementById('%= hdnForSelect.ClientID %>').value = "changed value";
}
Code behind
string changedValue = hdnForSelect.Value;

Related

Copying text from one text box to another using java script

I am hoping to get this fixed, so I have a textbox that's for initials and I want to copy that to the next 26 initial boxes so it's a bit easier for the user.
I want to do this on the Client-Side so I don't use up the performance on the server-side.
The issue I am having is that whenever I call the function that's supposed to copy the text over, somehow disables the textbox, I am unable to type anything in the textbox. Please see the code below, please let me know where I am going wrong!
<script type="text/javascript">
function copyText() {
var UI = document.getElementById("txtinitialOriginal").value;
document.getElementById("initial1").innerHTML = UI;
}
</script>
<input type="text" onkeypress="copyText(); return false" runat="server" id="txtinitialOriginal" style="font-size:20px" />
<asp:TextBox ID="initial1" placeholder="Initial Here" style="float:right" runat="server"></asp:TextBox>
What am I doing wrong?
Using InnerHtml will not set the value of a text box. InnerHTML replaces the HTML inside of the input.
Try setting the textbox with this value
document.getElementById("initial1").value = UI;
or
document.getElementById("<%= initial1.ClientID %>").value = UI;
Edit:
Your textbox is disabled because you have
onkeypress="copyText(); return false"
you need to return true

condition for an MVC checkbox in javascript

I have a checkbox in an MVC project called:
#Html.CheckBox("ShowAll", true)
and then in my js I want to check if the checkbox is checked or not.
Something like
function checkboxAll(item)
{
if ((showAll).isChecked)
{
//do stuff
whats the best way? I cant seem to get the syntax right
Thanks
}
}
When using #Html.Checkbox the first parameter is the html attribute "name" that will be applied when the checkbox is created.
If you retrieve a DOM element from a html attribute you can use the following jquery:
var isChecked = $("[Name='ShowAll']").val()
P.S. I can't guarantee this is correct and will edit comment later if it is.
So if you write #Html.CheckBox("ShowAll", true) this renders a checkbox with below properties.
<input checked="checked"
id="ShowAll"
name="ShowAll"
type="checkbox"
value="true" />
Also it generates a hidden input like this:
<input name="CheckBox" type="hidden" value="true" />
Now you know how to get a field by name or id in jquery.
access value by
$("input[name='ShowAll']").val()
or is checked
$("input[name='ShowAll']").is(':checked')
or by pure js var chkbox = document.getElementsByName("ShowAll");
See the msdn page for complete overloads.

How to give value of textbox to ngModel textbox using javascript

This is my first html textbox with javascript code
<div>
<input type="text" onchange="getTheValue(this)" id="12345" />
</div>
and this is my second html tag
<input type="hidden" id="12345_hiddenField" ng-model="hiddenField"/>
And on onchange event i am calling this javascript function
function getTheValue(data) {
document.getElementById("12345_hiddenField").value = data.value;
}
i want first textbox value to be assigned to second textbox ngmodel value with pure javascript no angualrjs methods in that, becoz that onchange function is written in seperate pure javascript file,is their anyway to do this?
I would do it like this.
document.getElementById("12345").addEventListener('change',function() {
document.getElementById("12345_hiddenField").value = document.getElementById("12345").value;
See fiddle https://jsfiddle.net/9fh8rxck/
<input type="text" ng-model="textboxValue" id="12345" />
and your hidden:
<input type="hidden" id="12345_hiddenField" value="{{textboxValue}}"/>
Your input text is bound to the $scope property textboxValue
Your hidden input uses that variable as value {{textboxValue}}, everything is 2-way data-bound.

Issue passing variable from javascript to c#

I am trying to pass the value of a variable from javascript to C#.
Here is the client side part:
<input type="text" value="1" id="h" name="n" runat="server" />
<script>
$("a").on("tap", function (event) {
var a = event.target.id;
$("#h").val(a);
});
</script>
and the server side part:
<%
string param = h.Value;
Response.Write(param);
%>
The input field has an initial value=1. A JQuery event changes the value attribute to, let's say, 5. When I try to access the value of input from server side (h.Value), the value is again, 1.
What am I doing wrong?
Thank you.
I beleive you need to use an ASP.NET TextBox control instead of a plain html input.

Setting a hidden field and accessing it in MVC4

I have the need to create a hidden field in of my table. Later upon button click, i need to retrieve the data saved in the hidden field. Below is the code:
<td>
<input id="hdnr<%=RowNumber%>c<%=ColumnNumber%>" type="hidden" value="{{Html.HiddenFor(model => item.Key)}}" />
</td>
Where rownumber and colnumber are variables.
Later in jquery, upon button click, I am attempting to retrieve the value of the hidden field as per the code below:
var value = $('#hdnr'+i+'c'+j).val();
alert(value);
Somewhere something is wrong. Either the value is not getting saved in the html tag or it is not being retrieved correctly.
Please help.
Thanks in advance.
Your value in the input is not correct. You can put the value directly from the model.
<input id="hdnr<%=RowNumber%>c<%=ColumnNumber%>" type="hidden" value="<% model.Key %>" />
if you want to use the helper, but in this case the id will be generated automatically. :
Html.HiddenFor(model => item.Key)
You can still add custom attribute. I'm not sure of the syntax :
Html.HiddenFor(model => item.Key, { #class = "myClass" })

Categories