This works great... But I am having one issue incorporating this with Ajax...
This is the text field to be edited...
Code:
<div class="editUse"><img src="ranking/usage/amy.gif"></div>
When I click on the div, the following Javascript process runs:
Code:
function editBox_use(actual) {
if(!changing) {
actual.innerHTML = fetch_usage(actual.innerHTML).join(',');
actual.innerHTML = "<input id=\""+ actual.id +"_field\" type=\"text\" value=\"" + actual.innerHTML + "\" onkeypress=\"return fieldEnter(this,event,'" + actual.id + "')\" onblur=\"return fieldBlur(this,'" + actual.id + "');\" style=\"width:50px; text-align:center; font-size:12px; \" />";
changing = true;
}
actual.firstChild.focus();
}
function fetch_usage(code) {
matches = new Array();
while (code.match(/(<img src="\w+\/usage\/(\w+?)\.gif">)/i)) {
matches.push(RegExp.$2);
code = code.replace(RegExp.$1, '', code);
}
return matches;
}
This works great, and the text box correctly fills out with "amy". After I edit the box, the following PHP script is run:
($input is the new value where "amy" once was, lets say for now it is "cas")
Code:
function update_score($input)
{
global $thumbdir;
$input = str_replace(' ','',$input);
$input = strtolower($input);
$uses = explode(',',$input);
foreach ($uses as $use)
{
$usage .= '<img src="'.$thumbdir.'/usage/'.$use.'.gif">';
}
echo $usage;
}
Running this script properly and returns the image for "cas" right on the page:
Code:
<div class="editUse"><img src="ranking/usage/cas.gif"></div>
However, now a new issue comes up... THEORETICALLY, if I click on the div AGAIN (without refresshing the page), it should return the text box filled out with the word "cas". It doesn't instead it gives me a blank text box. If I refresh the page, it works fine, but if I don't (which is the purpose of AJAX), the regex script fails.
Is there something wrong with the way the new HTML is echoed out that makes it fail the matching?