how to pass values inside javascript function along with this - javascript

This is my code where I pass value:
echo '<td style="width:10px;" ><input type="text" name="qty__'.$product_name.'" id="qty__'.$product_name.'" onkeyup="total_amounts(this,'.$product_name.');" /></td>';
i just only want onkeyup function take this and also product name which will be used in below code
<script>
function total_amounts(qty,product_id)
{
alert('hi');
var product_id = document.getElementById("discount1").value;
alert(product_id);
var price1 = document.getElementById("price1__"+product_id).value;
var discount1 = document.getElementById("discount1__"+product_id).value;
alert(discount1);
document.getElementById('net_price').value = price1;
}
</script>
but the problem is I have not received the product name inside the script.

Maybe you are forgetting to put strings in quotes?
Escaping ' by using a backslash \' allows you to do this.
onkeyup="total_amounts(this,\''.$product_name.'\');"

Related

Form response blank

I have some google script that generates an initial form then gathers a number does a lookup and then is supposed to return a second form (getfamily function). The second form which is dynamically generated returns blank. I can see the formHTML variable with data in the logger, but it comes up blank in the browser. Any suggestions would be appreciated.
var ssID="xxx";
var rows = SpreadsheetApp.openById(ssID).getSheetByName("studentinfo").getDataRange().getValues();
function doGet() {
var html = HtmlService.createTemplateFromFile('index2').evaluate()
.setTitle('Lookup').setSandboxMode(HtmlService.SandboxMode.NATIVE);
return html;
};
function getfamily(form){
Logger.log(form.familyid);
var ssID="xxxx";
var rows = SpreadsheetApp.openById(ssID).getSheetByName("studentinfo").getDataRange().getValues();
var formHTML = "<!DOCTYPE html>";
formHTML +="Hello!";
formHTML += '<form id="students">';
var filteredRows = rows.filter(function(row){
var message="made it";
if (row[0] === form.familyid) {
Logger.log(row[2]);
formHTML+= '<input type="checkbox" name ="students value='+ row[1] + '">'+ row[2] + '<br>';
return row[2];
}
});
formHTML+='<input type="submit" value="CheckIn">';
formHTML+='</form>';
Logger.log(formHTML);
var output = HtmlService.createHtmlOutput(formHTML).setSandboxMode(HtmlService.SandboxMode.NATIVE);
return output;
};
Your input type="checkbox" line is hard to figure out what you want. I presume that you plan in insert this form into an already exist DOM so no need the worrying about other tags just stick it in whatever div you have prepared for it.
function getfamily(form){
var ssID="xxxx";
var rows = SpreadsheetApp.openById(ssID).getSheetByName("studentinfo").getDataRange().getValues();
var formHTML='<form id="students">';
var message="made it";
rows.forEach(function(row){
if (row[0]==form.familyid) {
formHTTML=Utilities.formatString('<input type="checkbox" name="students" value="%s" /><br />',row[2]);//I presume that you want to change the name but I cant tell how you planned to do it.
}
});
formHTML+='<input type="button" value="CheckIn" onClick="proceesForm(this.parentNode);" />';
formHTML+='</form>';
return HtmlService.createHtmlOutput(formHTML);
};
You can use submit if you really must but I find using google.script.run to be a lot easier. We need to see more of what you're doing to provide a complete answer.

How can I paste a function parameter to be a string that have apostrophes inside of it

I have the function delete():
var array = new Array();
function deleteMessage(name, role, date, time,text) {
alert(text);
}
var message = array[i].message.replace(/'/g,"\\'");
And the input
<input id='delete' name='delete' type='submit' value='Delete' onclick='deleteMessage(\""+ array[i].name + "\",\""+ array[i].role + "\",\""+ array[i].date + "\",\""+ array[i].time + "\",\""+ message +"\")'>
How can my function last parameter text get a string that has inside of it some single quotes.
I tried replacing with .replace(/'/g,"\'"); , .replace(/'/g,"\\'"); , .replace(/'/g,"\\'");
And I tried replacing in the string like this "It\'s my life"
Nothing works, someone know how to do it?

Return a value created in an event handler inside a function from the same function

I have an input box and a go button. When the user clicks the go button I want to compare the inserted value with another value. I am trying to acquire the input value inside a function like so
function getInput(){
var entry ='';
$('button.go').on('click',function(){
var entry = $(this).siblings('.input').val();
//return entry
})
return entry
}
Basically I want to return the var entry that has an input value, so I could compare the values later in the code
var input = getInput() // this should have input value
is input > othervalue
I call getInput inside document.ready()
You are doing everything right . There are some scope related issues that is not helping you to get the expected result . My suggestion would be to define othervalue variable as global and check it inside the function like this
function getInput(){
$('button.go').on('click',function(){
var entry = $(this).siblings('.input').val();
if(entry>othervalue) //your code
});
}
I am not sure why you are binding dynamic click event inside a function . If there is nothing else you need to do here except this part, then wrap this piece of code inside document.ready.
I would suggest to declare your var entry =''; globally and assign it before comparing.
var entry="";
$('button.go').on('click',function(){
entry = $(this).siblings('.input').val();
});
//do the comparing..
You should do the comparing, after delegating the click function, inside the function.
var entry = "";
$('button.go').on('click', function(){
var entry = $(this).prev('.input').val();
if (entry < x)
// Do something
});
It remains unclear to me what you want to accomplish.
I made some code that might be somewhere near what you want.
jQuery().ready(function()
{
var startValue = $('input[name="the_input"]').val();
$('form').submit(function()
{
var currentValue = $('input[name="the_input"]').val();
$('body').append('<br />' + startValue + ' - ' + currentValue + ' = ' + (startValue - currentValue));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form action="#" method="post">
<input type="text" value="10" name="the_input" /><br />
<input type="submit" value="go!" />
</form>

Add line break to HTML from Js loop

I have a simple Js function that generates a list of random numbers based on how many the user wants. The function works fine, and logs fine, but it isn't displaying like I'd like it to. I'm new to Javascript, so I tried using the \n escape character, but it didn't do anything. Any help would be appreciated.
function generateIDs()
{
var num = document.getElementById('numberToGenerate').value;
var par = document.getElementById('numbers');
var button = document.getElementById('genButton');
button.disabled = true;
for (var x=0;x<num;x++)
{
var id = Math.floor((Math.random()*10000)+1);
par.innerHTML = id;
}
<form>
Auto-Generate <input type="text" name="number" id="numberToGenerate"/> IDs.
<button type="button" onclick="generateIDs()" id="genButton">Go!</button>
</form>
<p id="numbers">
</p>
\n doesn't mean much to a browser; use <br/> instead.
Example:
// snip
for (var x=0;x<num;x++)
{
var id = Math.floor((Math.random()*10000)+1);
par.innerHTML = id.toString() + '<br/>';
}
//snip
Note that this is going to overwrite the previous value on each iteration. You probably want this:
par.innerHTML += id.toString() + '<br/>';

can't get the second javascript function to work?

i can't get second javascript function to work.
when i click 'Send Mail' button it should call the second function and pass it these two values.
the href line (second last line in first function) is not rendered correctly.
<script>
function getvals(first,second) {
alert(''+first+'');
alert(''+second+'');
mywindow=window.open('','Send Mail','height=200,width=400');
mywindow.document.write("<FORM NAME='test'>");
mywindow.document.write("<table align='center'><tr><td>User/Group: </td><td><input type='text' id='newfirst' name='iuser'></td></tr>");
mywindow.document.test.iuser.value = ''+first+'';
mywindow.document.write("<tr><td>Issue Key: </td><td><input type='text' id='newsecond' name='ikey'></td></tr>");
mywindow.document.test.ikey.value = ''+second+'';
mywindow.document.write("<tr><td><a href='javascript:popitup(document.getElementById('newuser').value,document.getElementById('newkey').value);'>Send Mail</a></td></tr></table>");
mywindow.document.write("</FORM>");
}
function popitup(user,key) {
alert(''+user+'');
alert(''+key+'');
var url = 'http:\/\/localhost:8080/plugins/servlet/mailservlet?receiver=' + user + '&issue=' + key;
newwindow=window.open(url,'name','height=400,width=400');
if (window.focus) {newwindow.focus()}
}
</script>
The function popitup will not be called as it is written in the parent window, not in the window opened by window.open. Try window.opener in your function call.
Nested quotes problem, you sould correctly escape it:
mywindow.document.write("<tr><td><a href='javascript:popitup(document.getElementById(\"newuser\").value,document.getElementById(\"newkey\").value);'>Send Mail</a></td></tr></table>");
See it working
function getvals(first,second) {
alert(''+first+'');
alert(''+second+'');
mywindow=window.open('','Send Mail','height=200,width=400');
mywindow.document.write("<FORM NAME='test'>");
mywindow.document.write("<table align='center'><tr><td>User/Group: </td><td><input type='text' id='newfirst' name='iuser'></td></tr>");
mywindow.document.test.iuser.value = ''+first+'';
mywindow.document.write("<tr><td>Issue Key: </td><td><input type='text' id='newsecond' name='ikey'></td></tr>");
mywindow.document.test.ikey.value = ''+second+'';
mywindow.document.write("<tr><td>Send Mail</td></tr></table>");
mywindow.document.write("</FORM>");
}
function popitup(user,key) {
alert(''+user+'');
alert(''+key+'');
var url = 'http:\/\/localhost:8080/plugins/servlet/mailservlet?receiver=' + user + '&issue=' + key;
newwindow=window.open(url,'name','height=400,width=400');
if (window.focus) {newwindow.focus()}
}
Issues fixed:
1) Escaping problem in window.open
2) function call should be opener.popitup
3) Id names for user and key were incorrect.
please dont do it like this (document write)
use appendChild, and append the onclick-handler there

Categories

Resources