I'm trying change an input mask for textbox when the the check box has been check or unckecked but the problem that always is picking up the else condation only even the check box it is check or not.
please advice to fix this problem.
here is my code:
<%# Page Title="" Language="C#" MasterPageFile="~/Imam.Master" AutoEventWireup="true"
CodeBehind="WebForm4.aspx.cs" Inherits="Imam_Contacts.WebForm4" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script src="js/jquery-1.4.1.js" type="text/javascript"></script>
<script src="js/jquery.maskedinput-1.2.2.js" type="text/javascript"></script>
<script type="text/javascript">
if ($('#chkhtml:checked').size() > 0)
{
jQuery(function($) {
$("#txthtml").mask("999-99-9999");
});
} else {
jQuery(function($) {
$("#txthtml").mask("99/99/9999");
});
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<input id="chkhtml" type="checkbox" checked="checked" />
</asp:Content>
<script type='text/javascript'>
$('#chkhtml').click(function(){
if ($(this).attr('checked'))
$("#txthtml").mask("999-99-9999");
else
$("#txthtml").mask("99/99/9999");
});
$(function(){
$("#txthtml").mask("99/99/9999"); // this is default mask
});
</script>
I'm guessing you want to change the mask when checkbox changes its state but you don't know how you can achieve this.
Your code is running before the checkbox is rendered. Therefore, when the code runs, there are no checkboxes.
You need to wrap the entire if block in $(function() { ... }), which will execute the code in the function after the document finishes loading.
For example:
$(function() {
if ($('#chkhtml:checked').length > 0) {
$("#txthtml").mask("999-99-9999");
} else {
$("#txthtml").mask("99/99/9999");
}
});
Alternatively, you can use an inline conditional:
$(function() {
$("#txthtml").mask(
$('#chkhtml:checked').length > 0 ? "999-99-9999" : "99/99/9999"
);
});
Along with the other answers, here's an alternate way to do it...
<script type='text/javascript'>
$(document).ready(function() {
$('#chkhtml').click(function() {
if ($('#chkhtml').is(':checked')) {
$("#txthtml").mask("999-99-9999");
}
else {
$("#txthtml").mask("99/99/9999");
}
});
});
</script>
Related
(Warning: requirejs newbie) I'm trying to set my baseUrl for require.config but I'm getting an error on a very very simple setup. I've tried many different combos with slashes but no luck.
index.html
<!DOCTYPE html>
<html>
<head>
<title>Requirejs way</title>
<meta charset="utf-8" />
</head>
<body>
<h1><span id="foo">Hello World</span></h1>
<input id="Button1" type="button" value="button" />
<script data-main="common" src="Scripts/require.js"></script>
<script type="text/javascript">
require(['common'], function () {
//Load up this page's script, once the 'common' script has loaded
require(['home']);
});
</script>
</body>
</html>
common.js
/*
The first JS file to be loaded. Takes care of setting up all the required paths.
*/
// Configure RequireJS
requirejs.config({
baseUrl: "Scripts",
paths: {
jquery: [
//If the CDN fails, load it from the following
'jquery-3.1.1'
]
}
});
require(['home'], function (Methods) {
Methods.doSomething();
})
home.js
define(['jquery'], function ($) {
$('#Button1').on("click", function () {
$('#foo').text('[changed by jQuery]');
});
var Methods = {
doSomething: function () {
alert('i just did something');
}
};
return Methods;
});
Resource for code Go
After upgrading my project from ASP.NET MVC2 to MVC3, simply calendar partial view no longer renders. The code for this view is listed below.
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<AkwiMemorial.Models.CalendarModel>" %>
<link href="../../Content/jquery.ui.all.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../../Scripts/jquery-1.4.1.js"></script>
<script type="text/javascript" src="../../Scripts/jquery.ui.core.js"></script>
<script type="text/javascript" src="../../Scripts/jquery.ui.datepicker.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#datepicker').datepicker({
inline: true,
onSelect: function(dateText, inst) {
$('#DateStr').val(dateText);
$('form').trigger('submit'); }});
});
</script>
<% using (Html.BeginForm()) { %>
<div>
<div id="datepicker"></div>
<%= Html.HiddenFor(x => x.DateStr)%>
</div>
<% } %>
Any ideas? TIA.
Edit 1:
To see if this is a problem with my original project, I modified Home/Index view on a new MVC3 project as shown below. When I execute, I get error "Microsoft JScript runtime error: Object doesn't support this property or method". This is using jquery-1.5.1.min.js.
#model DateTime
#{
ViewBag.Title = "Home Page";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<script type="text/javascript">
$(document).ready(function () {
$('.date').datepicker({ dateFormat: "dd/mm/yy" });
});
</script>
#using ( Html.BeginForm())
{
#Html.TextBox("datepicker", Model.ToString("dd/MM/yyyy"), new { #class = "date" });
<input id="submit" name="submit" type="submit" value="Save" />
}
Why is this not working?
** Edit 2: **
Got jquery calendar to display in MVC3 using the following partial view:
#model DateTime
#using (Html.BeginForm())
{
<div id="datepicker">
#Html.TextBox("date", Model.ToString("dd/MM/yyyy"), new { #class = "date" })
</div>
}
<script type="text/javascript">
$(document).ready(function () {
$('#datepicker').datepicker({
inline: true,
onSelect: function (dateText, inst) {
$('#date').val(dateText);
$('form').trigger('submit');
}
});
});
</script>
Now back to the original problem.
I had the same problem and it fixed by upgrading to the latest version of jquery UI and jquery.
Without more info, my guess is that problem is with the nested div's. The default css of the browser could be playing a part here. Trying setting z-index of the datepicker div to a higher value.
Here is the full code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
<style type="text/css">
#import "jquery.countdown.css";
</style>
<script type="text/javascript" src="Scripts/jquery.countdown.js"></script>
<script type="text/javascript">
$('#shortly').countdown({ until: shortly,
onExpiry: liftOff, layout: "{ps} seconds to go"
});
$(document).ready(function () {
shortly = new Date();
shortly.setSeconds(shortly.getSeconds() + 5.5);
$('#shortly').countdown('change', { until: shortly });
});
function liftOff() {
// refresh the page
window.location = window.location;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<span id="shortly"></span>
</form>
</body>
</html>
I've got the jquery.countdown.js in the Scriptsmap of visual studio. Also the stylesheet "jquery.countdown.css" is in the project.
Don't have a clue about what the problem could be. I'm kind of new to jquery and trying to learn it.
I'm not familiar with this countdown plugin, but, try moving the part
$('#shortly').countdown({ until: shortly,
onExpiry: liftOff, layout: "{ps} seconds to go"
});
into the function passed to $(document).ready, replacing $('#shortly').countdown('change', { until: shortly });
Because otherwise the shortly var is not initialized when you're trying to use it.
you have a error in liftOff function with windowwindow.location
function liftOff() {
// refresh the page
window.location = window.location;
}
I have a problem to use jquery Plugin/Validation.
I want to add a method and follow the documentation but I think I still missing some thing.
First I add the method but I think I have a problem to implement it.
please check my code and advice me.
<script src="js/jquery-1.4.1.js" type="text/javascript"></script>
<script src="js/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
jQuery.validator.addMethod("domain", function(value, element) {
return this.optional(element) || /^http:\/\/yahoo.com/.test(value);
}, "Please specify the correct domain for your documents");
$("#aspForm").validate();
});
<asp:TextBox ID="TextBox1" runat="server" CssClass="domain" ></asp:TextBox>
</script>
I was able to find the right code for how to implement validation rule:
here is the code:
<script src="js/jquery-1.4.1.js" type="text/javascript"></script>
<script src="js/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
//Our validation script will go here.
$(document).ready(function() {
jQuery.validator.addMethod("domain", function(value, element) {
return this.optional(element) || /^http:\/\/yahoo.com/.test(value);
}, "Please specify the correct domain for your documents");
//validation implementation will go here.
$("#aspnetForm").validate({
rules: {
"<%=TextBox1.UniqueID %>": {
domain: true
}
}
});
})
</script>
i have this:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<script src="<%=Url.Content("~/Scripts/ckeditor/ckeditor.js")%>" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
var CurrName = $(".ckeditor").attr("name");
CKEDITOR.replace(CurrName, { width: "800" });
});
</script>
<span>Description</span>
<%=Html.TextArea("",Model,10,40,new{Class="ckeditor"}) %>
the strange thing the editor shows up just fine, but for some reason there is still the origanl textbox on the page ??? so i now have 2 EmployeeDescription
anyone got a clue?
Try replacing Class by #class:
<%= Html.TextArea("", Model, 10, 40, new { #class = "ckeditor" }) %>