/    Sign up×
Community /Pin to ProfileBookmark

Function doesn’t work

Hi!
I got a function which has to replace a regular element (as span or p) with a input element (las select or input).
This works great on firefux but i have many problems to fix on safari and ie.
The funcion is called by (and this is the ie reported mistake):

[code=html]<span class=”event” id=”domain” onclick=”morphElement(‘domain’, ‘morph=select;autosubmit=domainform;name=domain;options=myaudiens.com|cristianoinsola.it’);”>myaudiens.com</span>[/code]

And the function code is:

[CODE]function morphElement(id, actions) {
//Carico tutte le opzioni
var explode = actions.split(‘;’);
var option = new Array;
for(var n = 0; n < explode.length; n++) {
var query = explode[n].split(‘=’);
option[query[0]] = query[1];
}

//Azioni varie
var element = document.getElementById(id);
var parent = element.parentNode;
var node = new Object;
if(defaultNode == null) var defaultNode = element.innerHTML; // Valore originale
// CASELLA DI TESTO
if(option[‘morph’] == ‘text’) {
node = document.createElement(‘input’);
node.value = element.innerHTML;
node.name = option[‘name’];
node.type = ‘text’;
}

// TEXTAREA
if(option[‘morph’] == ‘textarea’) {
node = document.createElement(‘textarea’);
node.innerHTML = element.innerHTML;
}

// SELECT
if(option[‘morph’] == ‘select’) {
node = document.createElement(‘select’);
node.name = option[‘name’];
var loadOpts = option[‘options’].split(‘|’);
for(var i = 0; i < loadOpts.length; i++) {
var newOption = document.createElement(‘option’);
newOption.name = loadOpts[i];
newOption.value = loadOpts[i];
newOption.innerHTML = loadOpts[i];
if(element.innerHTML == loadOpts[i]) { newOption.selected = ‘selected’; var release = loadOpts[i]; }
node.appendChild(newOption);
}
}
node.name = option[‘name’];
node.id = id;

//Verifico se devo aggiungere tasto submit
if(option[‘submit’] != null) {
if(option[‘submit’] != ‘none’) {
var submitOriginal = document.getElementById(option[‘submit’]);
var submitButton = document.createElement(‘input’);
submitButton.id = ‘submit_’ + option[‘submit’];
submitButton.type = ‘submit’;
submitButton.value = submitOriginal.title;
submitOriginal.appendChild(submitButton);
} }
//Verifico invio automatico
if(option[‘autosubmit’] != null) {
if(option[‘autosubmit’] != ‘none’) {
node.onchange = function() { document.getElementById(option[‘autosubmit’]).submit(); }
} }
//Verifico completamento automatico
if(option[‘autofill’] != null) {
if(option[‘autofill’] != ‘none’) {
if(option[‘morph’] == ‘text’)
node.onkeyup = function() { document.getElementById(option[‘autofill’]).value = node.value; }
else if(option[‘morph’] == ‘textarea’)
node.onkeyup = function() { document.getElementById(option[‘autofill’]).value = node.value; }
else if(option[‘morph’] == ‘select’)
node.onchange = function() { document.getElementById(option[‘autofill’]).value = loadOpts[node.selectedIndex]; }
} }
//Reimposto valori di base
node.onblur = function() {
reverseElement(id, actions, element.tagName, element.getAttribute(‘class’));
if(submitOriginal != null) submitOriginal.removeChild(submitOriginal.firstChild);
}

parent.replaceChild(node, element);
}
//Funzione di reintegrazione
function reverseElement(id, actions, tag, class) {
var element = document.getElementById(id);
var parent = element.parentNode;
var node = document.createElement(tag);
node.id = id;
node.setAttribute(‘class’, class);
node.onclick = function() { morphElement(id, actions) }
if(element.tag == ‘textarea’) node.innerHTML = element.innerHTML; else node.innerHTML = element.value;
parent.replaceChild(node, element);
}[/CODE]

Can anybody help me? ?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@FangDec 15.2009 — Use of reserved words is the problem: "class"

Also setting the class name with setAttribute does not work in all browsers.
function reverseElement(id, actions, tag, classname) {
var element = document.getElementById(id);
var parent = element.parentNode;
var node = document.createElement(tag);
node.id = id;
node.className=classname;
node.onclick = function() { morphElement(id, actions) }
if(element.tag == 'textarea') node.innerHTML = element.innerHTML; else node.innerHTML = element.value;
parent.replaceChild(node, element);
}
×

Success!

Help @Wider 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.18,
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,
)...