Is it possible to update a ViewBag variable? - javascript

Can a viewbag variable be incremented using javascript method on a page? I have the following attempt:
function ConfirmFriend(friendID, addOrDecline) {
alert(addOrDecline);
if (parseInt(#ViewBag.ActualFriendCount) == 0 && addOrDecline == "add")
{
alert("THIS IS A FIRST ADD");
#{
int test = Convert.ToUInt32(#ViewBag.ActualFriendCount);
test++;
#ViewBag.ActualFriendCount == test;
}
}
var userID = '#ViewBag.UserID';
var postData = {
'userID': userID,
'userFriendID': friendID,
'addOrDelete': addOrDecline
};
$.post('/User/Show/', postData, function (data) {
});
document.getElementById("HiddenPendingFriends" + friendID).style.display = 'none';
if (addOrDecline == 'add') {
document.getElementById("HiddenAcceptedFriends" + friendID).style.display = 'block';
}
else {
document.getElementById("HiddenDeclinedFriends" + friendID).style.display = 'block';
}
};

Related

Get element of chaining in jquery to use in $.fn.{FunctionName}

I am creating a pluging based in Jquery and Bootrap (Laste Versions), that is call by this method/code:
$(document).find('.calendar-plug-bs').each(function(index, el) {
//$(this).plgCalendar("destroy"); Testing for destroy if exist...
$(this).plgCalendar();
})
it work with and element like this:
<div class="form-group row">
<div class="calendar-plug-bs"></div>
</div>
and this script/plugin starts as follows ...
(function($) {
$.fn.plgCalendar = function(param) {
return window.plgCalendar(param);
};
}(jQuery));
But the problem is that i need retrive node that call the initial function...
|------|
$(this).plgCalendar();
to add a table inside it ...
function plgCalendar(param = null) {
var r = null;
if (param !== null) {
if (typeof param.func !== 'undefined') {
if (param.func === "destroy") {
} else if (param.func === "getValue") {
}
} else {
console.log('%cMSG: (func) not Set', 'color: #bada55');
}
} else {
/*****WORKING ON THIS***/
var target = $(this);
console.log(target);
if (target.is("div")) {
var id = window.BuildRandID();
var $tableObject = $('<table/>', {
'class': 'bigger',
'id': id
});
$(target).append($tableObject);
return id;
} else {
window.alert("calendar-plug-bs must be a div");
}
/*****WORKING ON THIS***/
}
return r;
}
the fail is that var taget not is a div... and i dont know how to retrive...
resolve by this way:
(function($) {
$.fn.plgCalendar = function(param = null) {
param = window.plgCalendarParam(param);
param['this'] = $(this);
return window.plgCalendar(param);
};
}(jQuery));
function plgCalendarParam(param) {
if (param == null) {
param = [];
}
return param;
}
function plgCalendar(param) {
var r = null;
if (typeof param.func !== 'undefined') {
if (param.func === "destroy") {
} else if (param.func === "getValue") {
}
} else {
/*****WORKING ON THIS***/
var target = param['this'];
if (target.is("div")) {
var id = 'plgCalendar_' + window.BuildRandID();
var $tableObject = $('<table/>', {
'class': 'bigger',
'id': id
});
$(target).append($tableObject);
r = { 'id': id, 'init': true };
} else {
window.alert("calendar-plug-bs must be a div");
}
/*****WORKING ON THIS***/
}
return r;
}

value is not added to multidimensional array

The first time the function fires I get this result:
output1:test
The output2 2 alert is not firing. I know something is probably undefined in alert Does anyone know why the value won't be added in the multidimensional array?
I expect this to display after the false1:
output2:test2
Also if you want to fiddle with the code here it is:
https://jsfiddle.net/ndf0sjgf/1/
var carSelectedArray = [
[null]
];
addRow(carSelectedArray);
addRow(carSelectedArray);
function addRow(carSelectedArray) {
var arrayempty = false;
if (carSelectedArray[0][0] == null || carSelectedArray.length == 0) {
arrayempty = true;
} else {
arrayempty = false;
}
if (arrayempty == true) {
carSelectedArray[0][0] = "test";
alert("output1:" + carSelectedArray[0][0]);
} else {
carSelectedArray[1][0] = "test2";
alert("output2:" + carSelectedArray[1][0]);
}
}
Your loop works well, however your didn't define your array well.
there is only 1 dimension here :
var carSelectedArray = [[null]];
So replace with this :
var carSelectedArray = [[],[]];
PS : null is not required
and at the beginning in your function, you define arrayempty to false, so you can remove this :
else {
arrayempty = false;
}
Solution here : https://plnkr.co/edit/Qikalr0jc54R3MRSea4G?p=preview
var carSelectedArray = [[],[]];
addRow(carSelectedArray);
addRow(carSelectedArray);
function addRow(carSelectedArray) {
var arrayempty = false;
if (carSelectedArray[0][0] == null || carSelectedArray.length == 0) {
arrayempty = true;
}
if (arrayempty == true) {
carSelectedArray[0][0] = "test";
alert("output1:" + carSelectedArray[0][0]);
} else {
carSelectedArray[1][0] = "test2";
alert("output2:" + carSelectedArray[1][0]);
}
}

How do I set a javascript variable inside a for loop to be used afterwards

So, I'm creating a login form, and when certain criteria aren't met to continue after the form, I am setting a variable to be tested after I've tested all the criteria. IF that variable ($cantcontinue) is set to 'true' I want to send a console message with the criteria that isn't met. Here is my code:
function testfields() {
// Ask if logging in or Creating Account
//Logging In:
if (document.getElementById("tEmail").style.display != "unset") {
var loginelements = ["Username", "Password"];
var text = "";
var i;
for (i = 0; i < loginelements.length; i++) {
//Check all fields are full:
if (document.getElementById(loginelements[i]).value == "") {
document.getElementById(loginelements[i]).style.background = '#ff6060'
var cantcontinue = true;
console.log(loginelements[i] + " is not set,")
} else {
document.getElementById(loginelements[i]).style.background = '#f7f7f7'
}
}
if ($cantcontinue != true) {
console.log("Create Account")
} else {
console.log("Could Not Create Account")
}
//Create a new Account:
} else {
var createelements = ["Username", "Password", "tEmaili", "tConfirmi"];
var text = "";
var i;
for (i = 0; i < createelements.length; i++) {
//Check all fields are full:
if (document.getElementById(createelements[i]).value == "") {
document.getElementById(createelements[i]).style.background = '#ff6060'
var cantcontinue = true;
console.log(createelements[i] + " is not set,")
} else {
document.getElementById(createelements[i]).style.background = '#f7f7f7'
}
}
//If passwords Match
if (document.getElementById("Password").value != document.getElementById("tConfirmi").value) {
var cantcontinue = true;
document.getElementById("tConfirmi").style.background = '#ff6060'
document.getElementById("tConfirmi").value = ''
console.log(" Passwords didn't Match,");
}
if ($cantcontinue != true) {
console.log("Create Account")
} else {
console.log("Could Not Create Account")
}
}
}
$cantcontinue !== cantcontinue;//....
Also
if (cantcontinue != true) {
equals:
if (!cantcontinue) {
Sidenote: Please don't use "$" unless you're using jQuery. It reminds me of PHP ( brrrrr...)

How to select multiple checkboxes and fill a dropdownlist in mvc with javascript

I have 15 check boxes in MVC and i want to fill dropdownlist when any five of them are checked. with the help of JavaScript...please help me i am new to programming so i cant manage that to do.if you can help me with atleast javascript it is very helpful.
$(document).ready(function () {
$("#timeslotee").prop("disabled", true);
$("#Day").change(function () {
if ($("#Country").val() != "Select") {
var DocOptionss = {};
DocOptionss.url = "/Sample/timeslot";
DocOptionss.type = "POST";
DocOptionss.data = JSON.stringify({ dayy: $("#Day").val(), docname: $("#State").val() });
DocOptionss.datatype = "json";
DocOptionss.contentType = "application/json";
DocOptionss.success = function (StatesListd) {
$("#timeslotee").empty();
for (var i = 0; i < StatesListd.length; i++) {
$("#timeslotee").append("<option>" + StatesListd[i] + "</option>");
}
$("#timeslotee").prop("disabled", false);
};
DocOptionss.error = function () { alert("Error in Selecting A Day!!"); };
$.ajax(DocOptionss);
}
else {
$("#timeslotee").empty();
$("#timeslotee").prop("disabled", true);
}
});
});
public JsonResult Days(string docname)
{
var dd = db.USERs.FirstOrDefault(s => s.Full_name == docname);
List<string> StatesListw = new List<string>();
if (dd.DOCTOR.DOCTOR_DAYS.Monday)
{
StatesListw.Add("Monday");
}
if (dd.DOCTOR.DOCTOR_DAYS.Tuesday)
{
StatesListw.Add("Tuesday");
}
if (dd.DOCTOR.DOCTOR_DAYS.Wednesday)
{
StatesListw.Add("Wednesday");
}
if (dd.DOCTOR.DOCTOR_DAYS.Thursday)
{
StatesListw.Add("Thursday");
}
if (dd.DOCTOR.DOCTOR_DAYS.Friday)
{
StatesListw.Add("Friday");
}
if (dd.DOCTOR.DOCTOR_DAYS.Saturday)
{
StatesListw.Add("Saturday");
}
if (dd.DOCTOR.DOCTOR_DAYS.Sunday)
{
StatesListw.Add("Sunday");
}
return Json(StatesListw);
}
Are you using jQuery? If so, this should work:
var $checkboxes = $('input:checkbox');
$checkboxes.change(function(){
var numChecked = 0;
$checkboxes.each(function(index, element){
if(element.checked){
numChecked++;
}
});
if(numChecked >= 5){
//do something
}
});

How to convert a repetitive block of code to a function

I've got a sequence of code that will be run for the large majority of input items in a contact form; for the sake of lines of code, I want to have this operate as a function. Here is an example of the block:
$('#country_code').blur(function() {
var countryCode = $('#country_code').val();
if(validateNumber(countryCode) == true) {
if(countryCode.lastIndexOf('+') != 0) {
countryCode = countryCode.replace('+', '');
$('#country_code').val('+' + countryCode);
}
}
else {
countryCode = '';
$('#country_code').val(countryCode);
}
});
And I want to create a function like the following:
function validateElements(elementName, variableName, validationFunction, indexValue, indexPosition) {
$(elementName).blur(function() {
var variableName = $(elementName).val();
if(validationFunction(variableName) == true) {
if(variableName.lastIndexOf(indexValue) != indexPosition) {
variableName = variableName.replace(indexValue, '');
$(elementName).val(indexValue + variableName);
}
}
else {
variableName = '';
$(elementname).val(variableName);
}
});
}
In which I'd call the function like follows:
validateElements('#country_code', 'countryCode', 'validateNumber', '+', 0);
Try changing it so that you call your function as follows:
validateElements($('#country_code'), 'countryCode', 'validateNumber', '+', 0);
And the change your function:
function validateElements(obj, variableName, validationFunction, indexValue, indexPosition) {
obj.blur(function() {
var variableName = obj.val();
if(validationFunction(variableName) == true) {
if(variableName.lastIndexOf(indexValue) != indexPosition) {
variableName = variableName.replace(indexValue, '');
obj.val(indexValue + variableName);
}
}
else {
variableName = '';
obj.val(variableName);
}
});
}

Categories

Resources