can't get the second javascript function to work? - javascript

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

Related

how to pass values inside javascript function along with this

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.'\');"

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>

Change form input value with javascript

I am trying to change the input value of a hidden form to update the score of a game in my database.
I have this form code on a php page that displays and plays the game.
<form id ="recordForm" method="POST" action="updatePHP.php">
<input type='hidden' name="record" id='record' value='' />
</form>
And am trying to change the value of the hidden input field with this javascript. This is in the separate javascript file that is controlling the game.
function postPHP(newRecord){
alert("POST TO PHP"); //test to make sure I am calling this function
alert (newRecord); //alerts the correct value
var elem = document.getElementById('record');
elem.value = 12;
// document.getElementById('record').value = newRecord;
// document.getElementById('recordForm').submit();
};
There are a lot of topics on this subject but I am just not able to figure out what I am doing wrong. Any suggestions?
you should try
elem.value = newRecord;
Your JS function should work like this, i tested, more less what you already have. I remove the alerts since you don't need them anymore and leave what you have commented. This means your JS function isn't the problem.
function postPHP(newRecord)
{
document.getElementById('record').value = newRecord;
document.getElementById('recordForm').submit();
};
Don't forget to sent the parameter when calling the JS function, i did it with a button
<button onClick="postPHP('14')">Change</button>
since your JS function is in a separate file don't forget to include it in the File where you call the function
<head>
<script type="text/javascript" src="PATH/exampleName.js"></script>
</head>
Replace the src of the above tag to your needs
And last but not least check your updatePHP.php with a call to the method print_r
print_r($_POST);
All that should make the trick
Thank you for all your suggestions! This was my first question ever, I will look at all of them and see if I can get it working.
This is where I am calling postPHP:
function checkScore(score, record) {
alert('Score= ' + score);
alert ('Record= '+ record);
if(score < record || record === 0){
alert ("NEW RECORD"); //this alert is displayed when needed
postPHP(score);
}
};
and checkScore was called when the user moved a target crate back to the beginning spot and the following statement was executed
if (this.hasWon()) {
var finalScore = this.getScore();
var record = this.getRecord();
checkScore(finalScore, record);
return ret; //moving not allowed
}
there are some access methods used there.
//access methods
Board.prototype.hasWon = function() {
return state === 1;
};
Board.prototype.getScore = function() {
return score;
};
Board.prototype.getWt = function(r, c) {
return b[r][c];
};
Board.prototype.getData = function() {
return {"bobR": bobR, "bobC": bobC, "bobDir": bobDir,
"tgtR": tgtR, "tgtC": tgtC,
"startC": startC, "n": n};
};
Board.prototype.getRecord = function(){
var s = "" + window.location;
var ampIdx = "" + s.indexOf("&");
ampIdx = parseInt(ampIdx);
ampIdx = ampIdx + 7;
var record = "" + s.substring(ampIdx);
//alert("Puzzle Record= " + record);
record = parseInt(record);
return record;
}
;
I do have the javascript included. I do call it once in the body of the HTML, for some reason it doesn't display the game correctly when included in the head.
Again, thank you for the help! I will let you know what I get to work!
This is what I got to work.
function postPHP(newRecord, seed) {
alert("POST TO PHP");
var inner = "<input type='hidden' name='record' id='record' value=" + newRecord + " >"+
"<input type='hidden' name='seed' id='seed' value=" + seed + " >";
document.getElementById('recordForm').innerHTML = inner;
document.getElementById('recordForm').submit();
};
Thanks again for all the help, I just don't know why the first method wasn't working. This is my first attempts at PHP and javascript.

Call JS function after paste keyboard shortcut?

I'm trying to write a simple script that will take a list of urls that I need to save, append them to an array, and then spit that array back out in a div. The problem I'm having is that I am using ".onKeyUp" to call the function. It works, but when I use ctrl + v to paste, the function gets called twice. Once when you release the letter v and once when you release control. How can I fix it so that it will only call the function once when I'm using the keyboard shortcut? Here is my code:
<script type="text/javascript">
var arr = new Array();
function procession() {
var urlin = document.getElementById('url').value;
var smacky = "<br/> the url is: " + urlin + "<br/> the url is: www." + urlin;
arr.push(smacky);
document.getElementById('list').innerHTML = arr;
}
</script>
<form>
<input onkeyup="procession();" type="text" size="50" id="url"/>
</form>
<div id="list"></div>
Important Note: It's a long story, but I can't use jQuery for this script. Is there a way to do it in just pure javascript?
Pass in event to your onKeyUp, if the key is a ctrl, return false. This will only register one keyup event for shortcuts now:
<input onkeyup="procession(event);" type="text" size="50" id="url"/>
JS:
function procession(e) {
//if ctrl key, return false
if (e.which == 17) return false;
//do stuff
var urlin = document.getElementById('url').value;
var smacky = "<br/> the url is: " + urlin + "<br/> the url is: www." + urlin;
arr.push(smacky);
document.getElementById('list').innerHTML = arr;
}
It's a little hack-ish, but it should get the job done.
Here:
$(document).on("paste", function(event) {
alert("paste!");
});

call jquery function from javascript not working

I read similar posts on this but fails to work at this time. Mine is slightly different.
I call a javascript function addFormField which creates a dynamic input element within a form. This part works fine. Within that function I call the JQuery function loadData(id) to test if the id of the dynamically created element exists but it does not. Is loadData being called correctly to wait $ for the input element id to be created before checking if it's created? Thanks!
function addFormField() {
var id = document.getElementById("id").value;
$("#divTxt").append("<p id='row" + id + "'><label for='txt" + id + "'>Field " + id + " <input type='text' size='20' id='txt" + id + "'><p>");
$(function () {
loadData(id);
});
id = (id - 1) + 2;
document.getElementById("id").value = id;
}
function loadData(id) {
if ( $('#txt' + id).length ){
alert('success');
}
else {
alert ('fail');
}
}
<html>
<p>Add</p>
<form method="get" id="form1" action='#'>
<input type="hidden" id="id" value="1">
<div id="divTxt"></div>
</html>
Your code works fine for me: http://jsfiddle.net/6t8eX/
Just make sure that the addFormField() method is global. If it isn't, the inline onClick won't be able to find it.
If it is wrapped in $(function () { }), (or in any other function body) it won't be global.

Categories

Resources