Stan

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromStan
ToMe
Subjectadding and removing nodes to paragraph using a form
Date17 January 2005 22:24
Attachmentpage with sample script
First off I'd just like to thank for the awesome and of course informative
site. Here's my problem - i'm trying to create a dynamic page that has a
text field and that appends the input from the text filed to a paragraph
(that part works) ... the twist is that i also want to remove those nodes
using a link that is appended next to the text...(that part u guessed it -
dosent work ... well it sort of works)the final effort is to have a dynamic
page where i can build my inputs for a php script ... here's what happens -
all of the text i type in the field and click add appears in the paragraph
but i can only remove the very last node (not any node that has a remove
next to it) .. for some reason the remove link ids on all of the links get
set to the last ID ...why ? ...the source is below ... any help would be
greatly appreaciated as i'm running out of ideas .... thanx a lot in advance

stan
FromMe
ToStan
SubjectRe: adding and removing nodes to paragraph using a form
Date18 January 2005 8:50
Stan,

I think I see the cause of your problems.

removelink.onclick=function(){remove(parname,removelink.id);}

This is an event handler function. It does not have access to these
variables (parname and removelink). The only reason it does anything at all
is because these variables are global, so it always picks up the last one
that was stored, hence your problem. You need to ensure the function always
picks us the current ones that you are working on. To do that, use the
'new Function()' constructor instead of the inline function, and write the
string names in as strings (remove any linebreaks):
removelink.onclick=new Function('remove(\''+parname+'\',\''+removelink.id+'\');}
I have documented the use of this constructor on:
http://www.howtocreate.co.uk/tutorials/javascript/functions

This assumes that you don't put ' or \ or linebreaks in the strings. If you
do, you will need to convert them into valid strings, and that will get a
lot messier. So much so that I have built a utility to convert characters
like these into valid strings:
http://www.howtocreate.co.uk/tutorials/jsexamples/syntax/prepareInline.html

I am assuming that since this is for yourself, you will be able to remember
this and not put these characters in the strings.

A couple of other points:

removelink.setAttribute('ID',fivalue);
use 'id', not 'ID' - this should not make much difference it this current
code, but it is the correct way to do it :)

addedfield = document.createTextNode(fivalue+' \n');
the linebreak will not have any effect. remember, this is a HTML text node,
inside a paragraph. in HTML, all whitespace is displayed as a space. If you
want this to appear as a linebreak, either use <pre> instead of <p>, or
append a <br> onto the end of each (you will need to create a new 'br'
element node).


Hope this helps solve your problem

Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
FromStan
ToMe
SubjectRe: adding and removing nodes to paragraph using a form
Date18 January 2005 18:35
huge thanx for the tips ... I didnt realize that params for the onclick in
the remove links were global...I thought that automagically the correct
value would be bound to the last link and that link only  ..learn something
new everyday... that string definition of a function is an exact answer to
another problem i'm having... hmm i might have to do some string checking
and remove newlines and other funkey chars like u warned me as this form
has to be as retard proof as possible  (this will be a part of an internal
util for a produce company - this module is obviously still very very rough)
... once again huge thanx ..

stan
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.