javascript function call for multiple controls - javascript

I have a javascript function which I am calling for multiple controls,but it seems to be calling for the first ClientScript.RegisterStartupScript only.
ClientScript.RegisterStartupScript(this.GetType(), "myScript", "textCounter('" + txtCourseDesc.ClientID + "','" + txtRemDesc.ClientID + "', '2000')", true);
ClientScript.RegisterStartupScript(this.GetType(), "myScript", "textCounter('" + txtReqCourseCode.ClientID + "','" + txtRemCode.ClientID + "', '90')", true);
ClientScript.RegisterStartupScript(this.GetType(), "myScript", "textCounter('" + txtPReq.ClientID + "','" + txtPreqRem.ClientID + "', '1000')", true);
Please point out the problem.

You can't use same key for one function with different parameters. Try this:
ClientScript.RegisterStartupScript(this.GetType(), "myScript2000", "textCounter('" + txtCourseDesc.ClientID + "','" + txtRemDesc.ClientID + "', '2000')", true);
ClientScript.RegisterStartupScript(this.GetType(), "myScript90", "textCounter('" + txtReqCourseCode.ClientID + "','" + txtRemCode.ClientID + "', '90')", true);
ClientScript.RegisterStartupScript(this.GetType(), "myScript1000", "textCounter('" + txtPReq.ClientID + "','" + txtPreqRem.ClientID + "', '1000')", true);

Related

Asp.net post multiple parameters to javascript function

index.cs
If i post one value below method works.
Page.RegisterClientScriptBlock("Pass", "<script>pass('" + sle_password.Text.Trim() + "');</script>");
If i post multiple value like below it does not work
Page.RegisterClientScriptBlock("Pass", "<script>pass('" + sle_password.Text.Trim() + "','" + sle_username.Text.Trim() + "','" + txt_reseller_ID.Text.Trim() + "')";</script>");
JS Code:
function pass(pass, usrname, resellerId) {
alert(pass,usrnameresellerId);
}
Edited first code
Where i miss in second part code ?
try this
Page.RegisterClientScriptBlock("Pass", "<script>pass('" + sle_password.Text.Trim() + "','" + sle_username.Text.Trim() + "','" + txt_reseller_ID.Text.Trim() + "');</script>");

Javascript method argument escape

The below span tag containing an onclick event is not working
var test="<span onClick="gotoNode(\'' + result.name + '\',\'' + result.xaxis + '\',\'' + result.yaxis + '\',\'' + result.detail + '\',\'' + result.status + '\')" />"
the above escaped string has some problems with the method call.
How can I fix it?
If you're creating this in JavaScript to create an element, then the first single-quote needs to be escaped.
function gotoNode(name, xaxis, yaxis, detail, status) {
alert("name = " + name + "\r\nxaxis = " + xaxis + "\r\nyaxis = " + yaxis + "\r\ndetail = " + detail + "\r\nstatus = " + status);
}
var result = { name: "name", xaxis: "xaxis", yaxis: "yaxis", detail: "detail", status: "status" };
var htmlText = '<input value="Button" type="button" onclick="gotoNode(\'' + result.name + '\',\'' + result.xaxis + '\',\'' + result.yaxis + '\',\'' + result.detail + '\',\'' + result.status + '\')" />';
$("#lonely").append(htmlText);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="lonely"></div>
Generally speaking, whatever quote type you begin with (out of ' or "), you need to escape the same type that you want to use within the string and not escape the same type to terminate the string. You can leave the other type without escapes.
For your edited version, this should work if you want those result variables to be replaced with their values.
var test = "<span onclick=\"gotoNode('" + result.name + "','" + result.xaxis + "','" + result.yaxis + "','" + result.detail + "','" + result.status + "')\" />";
Do you use php to generate the output?
Then you should try
echo "<input type=\"button\" onClick=\"gotoNode(\" + result.name + \",\" +
result.xaxis + \",\" + result.yaxis + \",\" + result.detail + \",\" +
result.status + \")\" />";

How to call function with parameters as setTimeout callback?

How do you call a function with parameters via JQuery's setTimeout?
This "works", but the function is called immediately:
var successFunc = function(data, textStatus_ignored, jqXHR_ignored) {
alert("sf data='" + data + "', textStatus_ignored='" + textStatus_ignored + "', jqXHR_ignored='" + jqXHR_ignored + "', color_id='" + color_id + "'");
$('#toggle_color_like_cell_' + color_id).html(data);
};
var successFuncWithTimeout = function(data, textStatus_ignored, jqXHR_ignored) {
//alert("sfwt data='" + data + "', textStatus_ignored='" + textStatus_ignored + "', jqXHR_ignored='" + jqXHR_ignored + "', color_id='" + color_id + "'");
setTimeout(successFunc(data, textStatus_ignored, jqXHR_ignored), 2000);
}
This waits for two seconds, but according to the alert, all parameters are undefined:
var successFuncWithTimeout = function(data, textStatus_ignored, jqXHR_ignored) {
//alert("sfwt data='" + data + "', textStatus_ignored='" + textStatus_ignored + "', jqXHR_ignored='" + jqXHR_ignored + "', color_id='" + color_id + "'");
setTimeout(function(data, textStatus_ignored, jqXHR_ignored) {
alert("sf data='" + data + "', textStatus_ignored='" + textStatus_ignored + "', jqXHR_ignored='" + jqXHR_ignored + "', color_id='" + color_id + "'");
$('#toggle_color_like_cell_' + color_id).html(data);
}, 2000);
}
The parameters don't need to be passed to the anonymous function, because they're already in scope. (Thanks to this answer on Stack Overflow.)
var successFunc = function(data, textStatus_ignored, jqXHR_ignored) {
alert("sf data='" + data + "', textStatus_ignored='" + textStatus_ignored + "', jqXHR_ignored='" + jqXHR_ignored + "', color_id='" + color_id + "'");
$('#toggle_color_like_cell_' + color_id).html(data);
};
var successFuncWithTimeout = function(data, textStatus_ignored, jqXHR_ignored) {
//alert("sfwt data='" + data + "', textStatus_ignored='" + textStatus_ignored + "', jqXHR_ignored='" + jqXHR_ignored + "', color_id='" + color_id + "'");
setTimeout(function() {
successFunc(data, textStatus_ignored, jqXHR_ignored);
}, 2000);
}
Using the same concept, this also works:
var successFuncWithTimeout = function(data, textStatus_ignored, jqXHR_ignored) {
//alert("sfwt data='" + data + "', textStatus_ignored='" + textStatus_ignored + "', jqXHR_ignored='" + jqXHR_ignored + "', color_id='" + color_id + "'");
setTimeout(function() {
alert("sf data='" + data + "', textStatus_ignored='" + textStatus_ignored + "', jqXHR_ignored='" + jqXHR_ignored + "', color_id='" + color_id + "'");
$('#toggle_color_like_cell_' + color_id).html(data);
}, 2000);
}

Phonegap - inserting variable into database

I'm trying to insert variables values to a data base but it comes out as 'undefined'.
It is all inside device ready. I'm testing 1st with the variable 'absenceDateFrom'.
var absenceType = document.getElementById('absenceType')
var absenceText = document.getElementById('noteNewAbsence')
var absenceDateFrom = document.getElementById('absenceFrom')
var absenceDateTo = document.getElementById('absenceTo')
db.transaction(
function(tx) {
tx.executeSql("INSERT INTO absences (id,type,absenceFrom,absenceTo,note,aproved,picture) VALUES (" + id++
+ ",'"
+ String(absenceType.value)
+ "','"
+ absenceDateFrom.value.toString
+ "','"
+ absenceDateTo.value.toString
+ "','"
+ "random note (this works but if i put absenceText.value it comes out undefined also"
+ "',"
+ "'true'"
+ ","
+ "'image.jpg'"
+ ")");
}, transaction_error);
I also tried with '?' and [absenceDateFrom] in the end, like I saw on another answer and it still comes out undefined:
var absenceType = document.getElementById('absenceType')
var absenceText = document.getElementById('noteNewAbsence')
var absenceDateFrom = document.getElementById('absenceFrom')
var absenceDateTo = document.getElementById('absenceTo')
db.transaction(
function(tx) {
tx.executeSql("INSERT INTO absences (id,type,absenceFrom,absenceTo,note,aproved,picture) VALUES (" + id++
+ ",'"
+ String(absenceType.value)
+ "',"
+ "?"
+ ",'"
+ absenceDateTo.value.toString
+ "','"
+ "random note (this works but if i put absenceText.value it comes out undefined also"
+ "',"
+ "'true'"
+ ","
+ "'image.jpg'"
+ ")", [absenceDateFrom.value] );
}, transaction_error);
output:
http://prntscr.com/44eb31
Following is the assumptions that I have made about datatypes of the variables.
Number id;
String type,absenceFrom,absenceTo,note,aproved,picture;
Also aproved variable seems to be typo error. Should it not be approved
I have modified your tx() call
If there are any errors in the insert call. check the output of the errorInsert call
db.transaction(
function(tx) {
var sqlStmt = "INSERT INTO absences (id,type,absenceFrom,absenceTo,note,aproved,picture) VALUES ("
+ (id++) + ","
+ "'" + String(type) + "',"
+ "'" + String(absenceDateFrom) + "',"
+ "'" + String(absenceDateTo) + "',"
+ "'" + "random note (this works but if i put absenceText.value it comes out undefined also" + "',"
+ "'" + "true" + "',"
+ "'" + "image.jpg" + "',"
+ ")";
console.log("sqlStmt:"+sqlStmt);
tx.executeSql(sqlStmt,successInsert,errorInsert);
function errorInsert (err) {
console.log("ERROR CODE:"+err.code+":Message:"+err.message);
}
function successInsert(tx, result) {
console.log("SuccessInsert():Last inserted ID = " + result.insertId);
console.log("SuccessInsert():rowsAffected: " + result.rowsAffected);
}
}, transaction_error);
First: try to console.log(absenceDateTo) before inserting and be sure that the value is not undefined.
Second: In the line :
absenceDateTo.value.toString
You are passing the function context but you should call the function instead.
absenceDateTo.value.toString()
Try this ;)

onclick event and json data not meshing together

I've got a silly error which I cant seem to fix some how. I'm simply looking to do the following:
onclick="CreatePro('x','y','z')"
I basically want to pass text to the CreatePro function. Now my values of x,y and z are json data. As such here is what I am using for the javascript:
var Provision = "'" + data[i].ProvisionID + "'";
var Title = "'" + data[i].Title + "'";
var Author = "'" + data[i].Author + "'";
var Edition = "'" + data[i].Edition + "'";
var Publisher = "'" + data[i].Publisher+ "'";
var ISBN = "'" + data[i].ISBN + "'";
var UserID = "'" +data[i].UserID + "'";
var Price = "'" + data[i].Price+ "'";
var Condition = "'" +data[i].Condition +"'";
Row = Row + "<td><input type='button' onclick='CreatePro(" + Provision + "," + Title+ "," + Author + "," + Edition + "," + Publisher +"," + ISBN + ","+ UserID + ","+ Price + "," + Condition + ")' value='Create'></td></tr>";
console.log(Row);
Now when I use console.log I get the following:
<td><input type='button' onclick='CreatePro('19','dfjeryj','ertj','0','tj','0000000000000','4','0','0')' value='Create'></td>
But when I inspect the element I have :
<input type="button" onclick="CreatePro(" 19','dfjeryj','ertj','0','tj','0000000000000','4','0','0')'="" value="Create">
The problem is suspect is the " on the above line. But I dont know why this is happening? As from the console log. My quotes seem to match up. So I'm not sure why the browser is mixing them up? Perhaps I've gone wrong somewhere? (Frankly I can;t see the error). Every time I click the create button the event doesn't call my CreatePro function. I'm really not sure what I'm doing wrong or perhaps a better way of doing what I'm doing
You have got your quotes wrong. It should be:
Row = Row + "<td><input type='button' onclick=\"CreatePro('" + Provision + "','" + Title+ "','" + Author + "','" + Edition + "','" + Publisher +"','" + ISBN + "','"+ UserID + "','"+ Price + "','" + Condition + "')\" value='Create'></td></tr>";

Categories