I couldn't understand the code you've given, so I couldn't simplify it. Instead, I decided to code it from scratch. Here it is:
CODE
<p id="text" onclick="change();">Hello, world. Click me please.</p>
<form onsubmit="return false;">
<input type="hidden" id="textbox" />
<input type="hidden" id="done" onclick="changeBack();" value="Done"/>
</form>
<script type="text/javascript">
/*<![CDATA[*/
var textNode = document.getElementById('text');
var textValue = textNode.firstChild.nodeValue;
var textboxNode = document.getElementById('textbox');
var doneButton = document.getElementById('done');
function change() {
textboxNode.setAttribute('value', textValue);
textNode.style.display = 'none';
textboxNode.setAttribute('type','text');
doneButton.setAttribute('type','button');
}
function changeBack() {
textNode.firstChild.nodeValue = textboxNode.value;
textNode.style.display = 'block';
textboxNode.setAttribute('type', 'hidden');
doneButton.setAttribute('type','hidden');
}
/*]]>*/
</script>
This will do almost exactly what you wanted.
At first, the only thing visible will the paragraph, which contains the text "Hello, world. Click me please.". When it is clicked, the paragraph will become invisible, and the hidden textbox will appear and take its place. Then, you can edit the textbox. I don't think double-clicking can be detected by Javascript, so I made a button that can be pressed when you are done editing. When the button is pressed, the textbox and button disappear and the paragraph reappears. That is basically how the whole script works.
Now I'll explain what each of the lines do.
HTML:
The first line is the paragraph. The
onclick attribute is used to trigger the JavaScript.
The second line is the beginning of the form. The
onsubmit attribute is used to stop the form from submitting, therefore reloading the page.
Third and Fourth are the textbox and button. The
type="hidden" attribute is to make them invisible. Again,
onclick is used to trigger the JavaScript. And finally, the
value attribute is to add the text to the button.
java script:
The first 4 lines store different nodes into variables for faster access. Here are what they store, respectively: the paragraph, the text
inside the paragraph elememt, the textbox, the button.
In the first function
change(): In the first line, the textbox's text will be changed to the same as the paragraph's text. Then in the second line, the paragraph element is hidden from view. In the third, the textbox will change from a hidden input box to a textbox input box. In the fourth line, the button will change from a hidden input to a button input.
In the second function
changeBack(): In the first line, the paragraph's text is changed into what you entered in the textbox. In the second, the paragraph is set to
display: block, which basically makes it visible. In the third and fourth line, the textbox and button are both set back to hidden inputs again.
I hope you could understand this code. If you have any questions or if you want me to change the code, just reply and tell me.
I didn't know what you mean by "to have it in PHP". PHP can't be used to do interactive things in your browser.
Reply