call javascript on changing dropdown value programatically - javascript

I have a dropdown having n number of items, on selection of a particular item I make a tr visible. This I've done through JavaScript. Whenever a user changes the dropdown value the JavaScript on change event for the dropdown is fired. Till here its fine.
Problem:
When I set the value of the dropdown from my c# code the JavaScript event isn't fired, due to which the tr is not being visible. How can I address this problem???
Help !!!
Edit 1:
> ddlInform_Invite_Rating.Attributes.Add("onchange", "GetInviteRating('"
> + ddlInform_Invite_Rating.ClientID + "','" + txtInform_Invite_Score.ClientID + "');");
I've done dis in page load. The javascript is called when user changes the value. but when i set the dropdown's value (on page load) the javascript isnt called. I tried using using registerclientscript but that doesnt work as well.

I solved the above problem by including the following line in my JQuery:
$(document).ready(function () {
$("#<%=ctrl1.ClientID %>").change(function () {
//debugger;
if ($(this).val() != 0 && $(this).val() != 99) {
$(this).parent().closest('td').next().css("display", "table-cell");
$(this).parent().closest('td').next().next().css("display", "table-cell");
}
else {
$(this).parent().closest('td').next().css("display", "none");
$(this).parent().closest('td').next().next().css("display", "none");
}
}
);
$("#<%=ctrl1.ClientID %>").trigger('change');
}
);
Thanks for all your help anyway.

You can call javascript function by using below code.
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "key", "yourfunctionhere();", true);

Related

javascript onChange computation - show only one alert

I didnt know how to properly name my question, but here goes.
In my html i have a "form" but not
<form></form>
.It is just a couple of selects, radio buttons and text inputs.
I enter, check and select values and according to these values, some computation is done. This "form" is computing on every keydown, blur, change. So when I change one value it will immediately recalculate the results with new value.
I would like to alert the user, when he didnt fill any of the necessary inputs. Here is how it works now (this is in a separate .js file)
function calculator() {
// Here is code that gathers the data from html
// and here are also some computations (many if-s)
// The code is too long to be putted here
}
$(function () {
$('select, input').on('keydown blur change', calculator);
});
I tried to put a if statement inside of my calculator function:
function calculator() {
// Here is code that gathers the data from html
// and here are also some computations (many if-s)
// The code is too long to be putted here
if (val1 == '' && sadzba == '' && viem == '' && ...) {
alert('You have to fill all necessary fields!')
}
}
This obviously caused, that the alert was popped every time I enter / choose new value, because at the beginning all variables are empty / with no value.
So how can I achieve, this situation: User fills in the "form" except of (for example)one value and only then will the alert pop up.
Than you.
I suggest to do the check on submit and return false if one of the fields is empty, preventing the form to be submitted.
$('form').on('submit', function () {
if (val1 == '' || sadzba == '' || viem == '') {
alert('You have to fill all necessary fields!');
return false;
} else { return true; }
});
Use a different event handler for the onblur event since that's when the cursor has left the input box. (It also prevents the event handler from firing all the time. That's a pretty expensive process and it can slow your page down)
$('select, input').on('blur', didTheyLeaveTheFieldEmpty);
Hope I understood you right, you can try this:
function calculator(event) {
if ( $(event.target).val().length == 0 ) {
alert('fill the field');
}
}
$('select, input').on('keyup', calculator);
even if you don't want a form with a submit buton you can create a button
and trigger your code on it's click
<input type="button" class="calculate">
$(function () {
$('.calculate').on('click', calculator);
});

Why am I getting "DOM Exception 8" ,when using Jquery's unwrap()?

I have this simple small peace of code which generate an exception : (only in chrome)
Uncaught Error: NotFoundError: DOM Exception 8
Scenario : If a user visited a textbox and didn't put a value , he will be notify when bluring.
And the textbox will be wrapped with a red div. And if he clicks - (in order to put a value) - the red div is removed ( by unwrap the textbox).
So
I have a textbox.
This textbox has a blur handler attched.
When a blur occurs , I'm checking if a value has been entered.
if a value has been entered : everything is ok. nothing is done.
if not :
I alert the user
I wrap the textbox , with a red border
I attach a click event handler so that if the user clicks on the text box (in order to put a value )
the border disappears .
The problem :
When the user was alerted and a red-div wraps the textbox , every time I click on the textbox (in order to put a value) - Im getting the exception ( and obviously things are messed up)
Question :
Why is it happenning , and how can I fix it ?
The code : ( JSBIN )
$("body").on("blur", ".myTb", function () {
if (!$(this).val()) {
alert('you must enter something!');
doWork($(this))
}
});
function doWork(obj) {
var t = $("<div class='divError'/>");
obj.wrap(t);
obj.on('click', function () {
obj.unwrap();
});
}
p.s.:
1) I want to keep the idea of wrapping an element with a red div and to remove it when - user clicks (in order to put a value). (because this is a small part of my plugin which already works like this)
2) There is no problem in FF
3) chrome version : 25.0.1364.97 m
4) jquery: 1.8
I think the problem is, that you wrap the input field multiple times. As soon as you test if the error wrapper already exists, the error is gone:
$("body").on("blur", ".myTb", function () {
if (!$(this).val()) {
doWork($(this));
}
});
function doWork(obj) {
var t = $("<div class='divError'/>"),
hasError = obj.closest('.divError').length;
if(!hasError) {
alert('you must enter something!');
obj.wrap(t);
}
//console.log(obj);
obj.on('click', function () {
console.log(obj);
obj.unwrap();
obj.focus(); // be sure to set focus to the input element
});
}
Updated test is here.
The problem is that the blur event fires when you unwrap the input element. It's safer to use .one() in this case:
function blurOn()
{
$("body").one("blur", ".myTb", function () {
if (!$(this).val()) {
alert('you must enter something!');
doWork($(this));
}
});
}
function doWork(obj)
{
var t = $("<div class='divError'/>");
obj.wrap(t);
obj.one('focus', function () {
obj.unwrap().focus();
blurOn(); // enable blur event handler again
});
}
blurOn();
Demo

How do I make children of conditional form disappear and clear when a different parent is chosen?

Thanks for helping - I apologize ahead of time if my title was a bit confusing, Im having a hard time explaining the issue and even finding a solution.
Im trying to create a conditional form for a module that I'm currently building, but am paralyzed by my lack of true javascript knowledge.
*The code I'm currently working with can be found at:
http://jsfiddle.net/jgliesman/MQjf8/3/*
*What I would like the form to do can be found at:
http://form.jotform.us/form/22290232014135*
What Im trying to get the "mini code" to do is (like the Jotform)...
Clear the drop down/input selection if you change the parent. Right now the "mini code" is not hiding the 'grand-children' of the main parent when you switch to another parent. The direct child disappears but the 'grand-child' remains
I would also like it to reset the drop down/input selection if its hidden because if you pull it back up the selected option is still apparent its just hidden.
If you would like me to clarify anything please let me know, I really appreciate the help.
Thanks again,
Try this:
$(document).ready(function(){
$("#select1").change(function(){
if ($(this).val() == "1") {
$("#hide1").slideDown("fast");
} else {
$("#hide1").slideUp("fast");
$("#hide2").slideUp("fast");
$("#select2 option:first").prop('selected', true);
$("#select3 option:first").prop('selected', true);
}
});
$("#select2").change(function(){
if ($(this).val() == "1") {
$("#hide2").slideDown("fast");
} else {
$("#hide2").slideUp("fast");
}
});
});
Fiddle
i updated your fiddle: http://jsfiddle.net/MQjf8/4/
It does the same as #undifned suggested, but it also takes the reset into account.
$(document).ready(function(){
$("#select1").change(function(){
if ($(this).val() == "1" ) {
$('#hide1 select').val('(choose one)'); // select default
$("#hide1").slideDown("fast"); //Slide Down Effect
} else {
$("#hide1").slideUp("fast"); //Slide Up Effect
$("#hide2").slideUp("fast"); //Slide Up Effect
}
});
$("#select2").change(function(){
if ($(this).val() == "1" ) {
$('#hide2 select').val('(choose one)'); // select default
$("#hide2").val('(choose one)').slideDown("fast"); //Slide Down Effect
} else {
$("#hide2").slideUp("fast"); //Slide Up Effect
}
});
});

Why alert popup a few times after a click event?

On a page there are couple of Add buttons (li .plus).
When you click on a Add button and assume json.success is false, it will then popup via $.colorbox plugin
The popup pull the data from href:"/Handle/Postcode/?val" + Val
There is a submit button (#submitButton) from the popup, when I click on the submit button, it keep popup alert box a few times, I dont understand why that happen? how to fix it?
$("li .plus").click(function(event) {
event.preventDefault();
var Val;
Val = $('#id').val()
$.getJSON(Address +"/Handle/Add", {
Val:Val
}, function(json) {
if (json.success == "false" && json.error == "NoArea") {
$.colorbox({
width:"450px",
transition:"none",
opacity:"0.4",
href:"/Handle/Postcode/?val" + Val
});
$("#submitButton").live('click', function() {
var PostCodeArea = $("#deliveryAreaPostcode").val();
alert(PostCodeArea);
//Why does it popup a few times?
});
}
if (json.success == "true") {
Backet();
}
});
});
Thats an easy one, because you are using the .live() function to bind your click handler. If that code gets executed more than one time your binding happens more than one time.
You can either try to track the state of the binding and only apply it if it doesn't exist, or you can call your click function in the html with the onClick attr.
Edit: Just to clarify I meant something along the lines of -
HTML
<button id='submitButton' onclick="displayAreaCode();">Submit</button>
JS
function displayAreaCode(){
var PostCodeArea = $("#deliveryAreaPostcode").val();
alert(PostCodeArea);
}

how to hightlight a div if the page.isvalid=false

I wanna high light particular div when the page.isvalid=false.
on the pageload i am able to get display the error message. But the issue is that i am not able to highlight the control which is required.
I have a js function which sets the error class to the div. Its working fine when i click on a button. On the page load i wanna set this as well.
can i do this using some jquery??? or something other way..
Please let me know how to do this.... share your ideas....
Thanks
yes you can do this by using jquery
$(document).ready(callOnload);
function callOnload() {
//add code to highlight div
//following code is used by me to hightligh input control with has isreqired = true
$("input[isRequired=true]").each(
function(n) {
$('#' + this.id).addClass('mandatory');
if (this.value === '') {
$('#' + this.id).removeClass('normal');
$('#' + this.id).addClass('mandatory');
}
else {
$('#' + this.id).removeClass('mandatory');
$('#' + this.id).addClass('normal');
}
}
);
}

Categories

Resources