I want to set the value of a hidden field, using JQuery.
Hidden Field:
<input id="chag_sort" type="hidden" name="chag_sort">
My JQuery:
$("#input[name=chag_sort]").val(sort2);
What am I doing wrong? I should also mention in console that sort2 does in fact have a value: DESC.
The selector should not be #input. That means a field with id="input" which is not your case. You want:
$('#chag_sort').val(sort2);
Or if your hidden input didn't have an unique id but only a name="chag_sort":
$('input[name="chag_sort"]').val(sort2);
Drop the hash - that's for identifying the id attribute.
If you have a hidden field like this
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("VertragNr") %>'/>
Now you can use your value like this
$(this).parent().find('input[type=hidden]').val()
Related
I have an number input like this:
<input class="easyui-numberbox" data-options="precision:2,required:false" id="price3" name="price3" value="" />
But I can't get the input value with this:
$("input[name=price3]").numberbox('getValue')
But I can get the input value with ID attribute of input:
$("#price3").numberbox('getValue')
Should I define my all inputs an ID attribute to work with EasyUI?
Regards,
You can get an element with a specific attribute value as well. In your example try
$('input[numberboxname="price3"]').val()
The elements & the code.
HTML
<input value="" name="data[Filter][address]" type="text" />
<input value="" name="data[Filter][client]" type="text" />
<input value="" name="data[Filter][tenant]" type="text" />
<input value="" name="data[Filter][contract_end_date]" type="text" />
Javascript
console.log($("[name*='data\\[Filter\\]'][value!='']").serialize());
The problem: even if they are all empty, they are serialized.
Why?
You're looking at the value attribute. You can filter off of the value property instead:
http://jsfiddle.net/Y2P6w/
var $filledElems = $("[name*='data\\[Filter\\]']").filter(function () {
return $.trim(this.value).length;
});
The point is when the input tag gets inserted to the page, no matter it is in the page load or in your dynamic JavaScript codes, if it has the value attribute your selector query would use it or if you change your input's value using setAttribute in JavaScript or .attr() in jQuery, their value attribute actually gets changed, but if you change it with .value in JavaScript or .val() in jQuery or simply change the value in the page as a textbox, the attribute won't change, so you better not use value attributes in your selectors, because they are not reliable, an instead use $("[name*='data\\[Filter\\]']") and filter it as #JasonP has pointed out.
The runat="server" is breaking my jquery. I have two input, but for testing purpose added runat="server" in only one of those. Actually , I need to add on both.
Below you can find JS script to trigger the datetimepicker:
note: dateTo has runat="server" set and tried to change the way JS trying to get its ID, but still not working.
<script>
$(function(){
$("#dateFrom").datetimepicker();
$("#<%=dateTo%>").datetimepicker();
});
</script>
Here you can find the HTML input using runat="server" or not into asp.net code.
<tr>
<td>
<input type="text" id="dateFrom" name="dateFrom" value="" class="dateFrom" />
</td>
<td >
<input type="text" id="dateTo" name="dateTo" runat="server" value="" class="dateTo" />
</td>
</tr>
Does anybody has any idea,hint.....?
thank you
Use ClientID to get the generated Id of a server side control:
$("#<%=dateTo.ClientID%>").datetimepicker();
ASP.NET will generate a specific id attribute that is different from the id attribute of the server side control, so you need to use the generated value in order to access it from jQuery.
Since you're supplying class names, I suggest simply using those.
$(function(){
$(".dateTo, .dateFrom").datetimepicker();
});
Alternatively, you could give any "date" field on your form a class of "date" and use that:
$(function(){
$(".date").datetimepicker();
});
This is a common pattern for client-side validation, and also allows you to provide context clues through styling with CSS.
If you're using .NET 4 you can set the ClientIDMode attribute to Static. This will prevent the framework from changing the element's ID:
<input type="text" id="dateTo" name="dateTo" runat="server"
ClientIDMode="Static" class="dateTo" value="" />
ASP.NET will treat the inputs as server-side controls when runat=server is added, and this will result in transformed identifiers of those inputs so that they start with a container prefix (something like ctl00_), hence 'breaking' your jQuery selectors.
If you're using .NET 4 then you can disable these transformations by altering the ClientIDMode. Otherwise you will need to include the prefix in your selectors, or refactor your selectors to be independent of ID and somewhat distinct for selection by other means.
Use this to get correct client id for server controls
$('[id$=dateTo]').datetimepicker();
I have a table with id 'tbl' which contains textbox controls with id's like below
<table id="tbl">
txt1Text1
txt1Text2
txt2Text1
txt2Text2
txt3Text1
txt3Text2
.................
.................
I want to set the specif value in the textboxes that have id ends with Text1
I want to do it using jquery/javascript.
Thanks for help.
You can use Attribute Ends With selector.
$('#tbl input[type=text][id$=Text1]').val('new value')
You should add a fake css class, that allows you to "tag" the textboxes, then use jQuery to find these textbox using the css class selector.
<asp:TextBox runat="Server" CssClass="existingClass FakeClass" id="txt1" />
<asp:TextBox runat="Server" CssClass="existingClass FakeClass" id="txt2" />
<asp:TextBox runat="Server" CssClass="existingClass FakeClass" id="txt3" />
<script type="text/javascript">
$(function(){
$(".FakeClass").val("42");
});
</script>
What is important here, is that the "FakeClass" does not have to exists. It's only a marker.
$("input[id $= Text1]").val('your value');
Try with this
$('#tbl input[id$="Text1"]').val('my value');
Attribute Ends With Selector
Selects elements that have the specified attribute with a value ending exactly with a given string. The comparison is case sensitive.
$('input[type=text][id$=Text1]').val('value');
Hi
While working on asp, i want if i click on my asp text field, a radio button should be checked, but i dont find any onClick event available for asp text field, and if i use onchange, it is from the server end, i want this situation without page refresh, just like we do in html fields using javascript, please help!!!
Try this one:
HTML:
<asp:TextBox ID="txtSelect" runat="server" Width="200px" Text="Click here"> </asp:TextBox>
<br />
<asp:RadioButton ID="radCheck" runat="server" />
JQUERY:
$("#txtSelect").click(function(){
$("#radCheck").attr("checked",true);
});
OnFocus is probably the event I would use.
You can use Ajax if you don't wanna refresh the page