/    Sign up×
Community /Pin to ProfileBookmark

problem removing input field node in IE

I’m working on a project that is meant to do the following:

When you click on a div on the page, the contents of the div are copied out, an input field with type = text is placed inside the div, then the previous contents of the div are placed inside the input field. When you click on the div again, the contents of the input field are copied out, the input field is removed, and the previous contents from the input field are copied back into the div.

Essentially, it’s meant to allow you to edit the contents of the div. The code that I have works flawlessly in Firefox and Chrome, but when I try it in IE 8, the second click simply removes the contents of the input field and leaves the input field in place. It is removing the node’s value rather than removing the node.

Here are my javascript functions:

[CODE]
function editField(strID, strField, strTable)
{
var el = document.getElementById(strID + strField);
var strText = el.firstChild.nodeValue;
el.removeChild(el.firstChild);

document.getElementById(strID + strField).setAttribute(“onClick”, ‘updateField(” + strID + ”, ” + strField + ”, ” + strTable + ”)’);

eInput = document.createElement(“input”);
eInput.setAttribute(“type”, ‘text’);
eInput.setAttribute(“id”, ‘editField’);
eInput.setAttribute(“value”, strText);

document.getElementById(strID + strField).appendChild(eInput);
}

function updateField(strID, strField, strTable)
{
var el = document.getElementById(‘editField’);
var strText = el.value;
el.parentNode.removeChild(el);

document.getElementById(strID + strField).setAttribute(“onClick”, ‘editField(” + strID + ”, ” + strField + ”, ” + strTable + ”)’);

document.getElementById(strID + strField).appendChild(document.createTextNode(strText));
}
[/CODE]

and here is my div:

[CODE]
<div id=”myDiv” onClick=”editField(‘mydiv’, ‘fieldName’, ‘tableName’)”>Text inside my div</div>
[/CODE]

Any thoughts?

Thanks!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@tirnaFeb 16.2011 — It's quicker for me to write my own code than to debug someone else's and so I came up with this. It works in IE8 and FF3.6. The only minor difference is that I put an onblur on the textbox, so you need to click anywhere off the textbox when you have finished editing the text to transfer the contents of the textbox back to the div.

[CODE]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd[/URL]">
<html xmlns="[URL]http://www.w3.org/1999/xhtml[/URL]">
<head>
<title></title>
<script type="text/javascript">
var inputTxt = document.createElement('input');
inputTxt.type = 'text';
inputTxt.onblur=function(){
var txt = this.value;
removeChildren(this.parentNode);
oDiv1.innerHTML = txt;
editMode = 'off';
}
window.onload=function(){
editMode = 'off';
oDiv1 = document.getElementById('div1');
oDiv1.onclick=function(){
if (editMode == 'off') {
inputTxt.value = this.innerHTML;
removeChildren(this);
this.appendChild(inputTxt);
editMode = 'on';
}
}
}
function removeChildren(obj){
var children = obj.childNodes;
for(i=0; i < children.length; i++){
obj.removeChild(children[i]);
}
}
</script>
</head>
<body>
<div id="div1">
This is div1
</div>
</body>
</html>
[/CODE]
Copy linkTweet thisAlerts:
@freaknutauthorFeb 16.2011 — I feel a bit silly now...

tirna - I noticed you had the doctype in your example and realized that I was missing that from my page. Once I added the doctype to the page, it actually fixed my issue. No code changes required!

Thanks for your response!
×

Success!

Help @freaknut 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 5.20,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...