onclick event and json data not meshing together - javascript

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>";

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 + \")\" />";

Can someone resolve the error in this code

rs = stmt.executeQuery("select c1.itemname,c1.itemcalorie,p.restname,p.location from categorie1 c1 ,Place p
where p.pincode = '" + pincode1 + "' and c1.itemid IN
("select c1.itemid from categorie1 c1 where c1.itemcalorie <='" + cal1 + "' and c1.itemcalorie >='" + cal1-400 + "' and c1.restid = p.restid ") ");
You have too many extra quotes:
rs = stmt.executeQuery(
"
SELECT c1.itemname,
c1.itemcalorie,
p.restname,
p.location
FROM categorie1 c1,
place p
WHERE p.pincode = '" + pincode1 + "'
AND c1.itemid IN (SELECT c1.itemid
FROM categorie1 c1
WHERE c1.itemcalorie <= '" + cal1 + "'
AND c1.itemcalorie >= '" + cal1-400 + "'
AND c1.restid = p.restid)
"
);
Im not sure wihch language is this (I hope it is not javascript) but the code is not safe look into parametrized query.

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 ;)

Javascript innerHTML not allowing onClick on javascript function

I am not sure why, but the following
href='javascript:"+ openextlink('http://www.ipetfindr.com/shop/product/' + item._id.$id);+"'
seems to run automatically without the user clicking.
document.getElementById("shop-items").innerHTML += "<div class='product " + cssclass + "'><div class='product-images-smaller'><span class='shop-large-image'><img src='" + item.pictures[0] + "'/></span></div><h1>" + item.name + "</h1><div class='prodtext'><b>Status:</b> " + item.status + "<br><b>Price:</b> $" + item.price + "<br><a id='shop_" + item._id.$id + "' href='javascript:"+ openextlink('http://www.ipetfindr.com/shop/product/' + item._id.$id);+"'><h3 id='dshop_" + item._id.$id + "' class='green_button'>Buy Now</h3></a></div></div>";
could anyone please tell me why.
href='javascript:"+ openextlink('http://www.ipetfindr.com/shop/product/'
I see a double quote , is it a typo, change to
href='javascript:'+ openextlink('http://www.ipetfindr.com/shop/product/'

Categories