/    Sign up×
Community /Pin to ProfileBookmark

Multiple image buttons problem

Hi

I’m trying to assign the enter key press to one particular image button on the page.
I have a div generated via an ajax call to my php page that populates the div from the db – this brings up a list of text boxes that can be updated and are associated with image buttons and at the bottom a text field to add a new item and an “add” image button – it’s this one I want to fire when the return key is pressed, however what actually happens is the first update image button event fires (which sort of makes sense, only not what I want to happen ?

All of the buttons update/insert/delete the db when clicked , only I want the only button to respond to the return key press to be the “add” one – I don’t want it to submit, just call my javascript add function which refreshes the div on db success and displays the updated list.

the relevant code:-

[CODE]while($row = mysql_fetch_array($keywlistres, MYSQL_ASSOC))
{
$act= $row[‘activity’];
$aid= $row[‘a_id’];
$akid= $row[‘ak_id’];
$keyw[$ctr] = $row[‘keyword’];
echo “<tr>”;
echo “<td class = “tdwhitebg” >$act</td>”;
echo “<td class = “tdwhitebg” >$akid<input type = “text” id = “editkw” “width =”20″ value =”$keyw[$ctr]”></td>”;
echo “<td class = “tdwhitebgl” ><input type = “image” src=”img/update.gif” id = “updatekw” onclick=”updateKw($akid,’update’,$ctr,$aid);”></td>”;
echo “<td class = “tdwhitebgl” ><input type = “image” src=”img/delete.gif” id = “deletekw” onclick=”deleteKw($akid,’delete’,$aid)”></td>”;
echo “</tr>”;
$ctr++;
}
echo “<tr>”;
echo “<td class = “tdwhitebg”>&nbsp;</td>”;
echo “<td class = “tdwhitebg”><input type = “text” name = “kw” id = “addkwtextbox” width =”20″ onkeyup=”checkKey()”></td>”;
echo “<td class = “tdwhitebg”><input type = “image” id = “addkw” src=”img/add.png” onclick=”insertKw($aid,’insert’,kw.value)”></td>”;
echo “<td class = “tdwhitebg”>&nbsp;</td>”;
echo “</tr>”;[/CODE]

As I’m a complete newbie to javascript I’m finding this frustrating – can’t seem to find a similar problem referenced on a web search, all my javascript (not quoted works fine in so far as it does what I expect – just want the insertKw($aid,’insert’,kw.value) to fire when the user hits the return key! Note that I don’t want the image button to act as a submit button, just to fire the function off.

I guess what I’m looking for is something that detects that the user is in the addkwtextbox and then “binds” a return key press to the image button with id addkw, just too new to javascript to know where to start looking on this

Here’s hoping you wise guru’s can help!

Actually there is another problem I’m having with this app but I’ll save that for a separate post as (I think) is unrelated… troubles with IE vs FF3…

Thanks in advance – The(gettingincreasingly)MadProfessor

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@shrewd_houndSep 09.2008 — in the head add

<script type="text/javascript">

function ifEnter(var1,var2,var3,var4) {

if ((event.which && event.which == 13) || (event.keyCode && event.keyCode== 13))

{

updateKw(var1,var2,var3,var4);

}

return true;

}

</script>


then update your input text field to look like

echo "<td class = "tdwhitebg" >$akid<input type = "text" id = "editkw" "width ="20" value ="$keyw[$ctr]" onkeypress="javascript:ifEnter($akid,'update',$ctr,$aid)"></td>";

good luck


This should fire the update call of the updateKw function
Copy linkTweet thisAlerts:
@TheMadProfessorauthorSep 09.2008 — Tried adapting this (it's the last text input (id=addkwtextbox) that I want to test for the enter key so changed

[CODE]echo "<td class = "tdwhitebg" >$akid<input type = "text" id = "editkw" "width ="20" value ="$keyw[$ctr]" onkeypress="javascript:ifEnter($akid,'update',$ctr,$aid)"></td>";[/CODE]

to

[CODE]echo "<td class = "tdwhitebg"><input type = "text" name = "kw" id = "addkwtextbox" width ="20" onkeypress="ifEnter($aid,'insert',this.value);></td>";[/CODE]

and

[CODE]function ifEnter(var1,var2,var3,var4) {
if ((event.which && event.which == 13) || (event.keyCode && event.keyCode== 13))
{
updateKw(var1,var2,var3,var4);
}
return true;
}[/CODE]


to

[CODE]function ifEnter(var1,var2,var3) {
if ((event.which && event.which == 13) || (event.keyCode && event.keyCode== 13))
{
updateKw(var1,var2,var3);
}
return true;
}[/CODE]


to reflect the input fields I was after... no joy sadly, although I think I see where you were going with this and am at a loss to see why it's not working...

being new to JS I don't get the event.which && event.which == 13 [/QUOTE]bit though.
Copy linkTweet thisAlerts:
@TheMadProfessorauthorSep 09.2008 — You've done the biz.

Was a typo in my cutnpasting so now the last text box looks like this:-
[CODE]echo "<td class = "tdwhitebg" colspan="2"><input type = "text" name = "addkwtextbox" id = "addkwtextbox" width ="20" onkeypress="ifEnter($aid,'insert',addkwtextbox.value);"></td>";[/CODE]

and the function like so:-
[CODE]function ifEnter(var1,var2,var3) {
if ((window.event.which && window.event.which == 13) || (window.event.keyCode && window.event.keyCode== 13))
{
insertKw(var1,var2,var3);
//alert("enter pressed!")
}
return true;
}[/CODE]


Many thanks again - JS I'm finding a bit peculiar to start off with, hope this has nudged me up the learning curve a little! ?

I'm too tired tonight to get into the problem I'm having with Firefox (app works ok in IE which'll do for now seeing as it's an admin page) unless anyone can see anything in the code posted so far which would cause FF to throw a wobbly and just submit/refresh the form on the 2nd update/delete?

the full code of my JS include now looks like this...
[CODE]
var xmlHttp;
var xmlHttp2;

function ifEnter(var1,var2,var3) {
if ((window.event.which && window.event.which == 13) || (window.event.keyCode && window.event.keyCode== 13))
{
insertKw(var1,var2,var3);
//alert("enter pressed!")
}
return true;
}

function showDiv(divid,param1)
{
//alert ("got to showDiv! "+divid+" "+param1);
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
switch (divid)
{
case "actlist":
{
var url = "getact.php?dummy=dummy";
//alert (url);
var div = "actlist";
break;
}
case "keywords":
{
var url = "showkeywords.php?actid="+escape(param1);
var div = "keywords";
break;
}
}
url=url+"&sid="+Math.random();
//alert (url);
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=stateChanged(divid);
xmlHttp.send(null);
}

function showDiv2(divid,param1)
{
//alert ("got to showDiv! "+divid+" "+param1);
xmlHttp2=GetXmlHttpObject2();
if (xmlHttp2==null)
{
alert ("Your browser does not support AJAX!");
return;
}
switch (divid)
{
case "actlist":
{
var url = "getact.php?dummy=dummy";
//alert (url);
var div = "actlist";
break;
}
case "keywords":
{
var url = "showkeywords.php?actid="+escape(param1);
var div = "keywords";
break;
}
}
url=url+"&sid="+Math.random();
//alert (url);
xmlHttp2.open("GET",url,true);
xmlHttp2.onreadystatechange=stateChanged(divid);
xmlHttp2.send(null);
}

function updateKw(akid,todo,ctr,aid,keyword)
{
xmlHttp=GetXmlHttpObject();
var keyword = document.getElementsByName("editkw")[ctr].value;
var url = "updatekw.php?kwid="+escape(akid)+"&todo="+escape(todo)+"&keyword="+escape(keyword);
url=url+"&sid="+Math.random();
//alert (url);
var div = "status";
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=stateChanged('status');
xmlHttp.send(null);

var url2 = "showkeywords.php?actid="+escape(aid)+"&sid="+Math.random();
//alert (url2);
xmlHttp2=GetXmlHttpObject2();
xmlHttp2.open("GET",url2,true);
xmlHttp2.onreadystatechange=stateChanged2('keywords');
xmlHttp2.send(null);
}
function deleteKw(akid,todo,aid)
{
xmlHttp=GetXmlHttpObject();
var url = "updatekw.php?kwid="+escape(akid)+"&todo="+escape(todo);
url=url+"&sid="+Math.random();
//alert (url);
var div = "status";
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=stateChanged('status');
xmlHttp.send(null);

var url2 = "showkeywords.php?actid="+escape(aid)+"&sid="+Math.random();
//alert (url2);
xmlHttp2=GetXmlHttpObject2();
xmlHttp2.open("GET",url2,true);
xmlHttp2.onreadystatechange=stateChanged2('keywords');
xmlHttp2.send(null);
}
function insertKw(aid,todo,keyword)
{
xmlHttp=GetXmlHttpObject();
var url = "updatekw.php?actid="+escape(aid)+"&todo="+escape(todo)+"&keyword="+escape(keyword);
url=url+"&sid="+Math.random();
//alert (url);
var div = "status";
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=stateChanged('status');
xmlHttp.send(null);

var url2 = "showkeywords.php?actid="+escape(aid)+"&sid="+Math.random();
//alert (url2);
xmlHttp2=GetXmlHttpObject2();
xmlHttp2.open("GET",url2,true);
xmlHttp2.onreadystatechange=stateChanged2('keywords');
xmlHttp2.send(null);
}

function stateChanged(divid)
{
return function()
{
if (xmlHttp.readyState==4)
{
document.getElementById(divid).innerHTML=xmlHttp.responseText;
}
else document.getElementById(divid).innerHTML = "<b>updating, please wait...</b>"+xmlHttp.readyState;
}
}
function stateChanged2(divid)
{
return function()
{
if (xmlHttp2.readyState==4)
{
document.getElementById(divid).innerHTML=xmlHttp2.responseText;
}
else document.getElementById(divid).innerHTML = "<b>updating, please wait...</b>"+xmlHttp2.readyState;
}
}

function GetXmlHttpObject()
{
//alert ("XmlHttpObjectInitialised!")
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari, Chrome
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}

return xmlHttp;
}

function GetXmlHttpObject2()
{
//alert ("XmlHttpObject2 Initialised!")
var xmlHttp2=null;
try
{
// Firefox, Opera 8.0+, Safari, Chrome
xmlHttp2=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp2=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp2=new ActiveXObject("Microsoft.XMLHTTP");
}
}

return xmlHttp2;
}[/CODE]


...which I am well aware is probably not the way to do ajax requests (certainly looks ugly to me!), but hey, I'm learning on the fly:eek:

Maybe I should look at mootools or jquery to tidy this stuff out of my JS, but I'd like to understand it 1st!

2 functions for each ajax call as they seem to get hung up with each other if not...
×

Success!

Help @TheMadProfessor spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...