473,626 Members | 3,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

element attribute affectation behaves like a move instead of a copy

Hi,

Assume that right and left are multiple select elements. It's about the
following line :

right.options[i]=left.options[j];

It copies the content of left.options[j] into right.options[i], but
*removes* the content of the first one !

It's hard to google with relevant keywords. Do you know if it's a
normal behaviour of Javascript ?

Tested on Firefox 1.5.x/Windows.

Regards,

--
Mounir.

Sep 18 '06 #1
13 1830

Mounir wrote:
Hi,

Assume that right and left are multiple select elements. It's about the
following line :

right.options[i]=left.options[j];

It copies the content of left.options[j] into right.options[i], but
*removes* the content of the first one !

It's hard to google with relevant keywords. Do you know if it's a
normal behaviour of Javascript ?
Not really a matter of JavaScript but, of the particular DOM being
manipulated by the javascript. I see the behavior you described in
Firefox but, in IE6 it simply removes from the right list.
Tested on Firefox 1.5.x/Windows.

Regards,

--
Mounir.
What is it that you are actually trying to do ?

Sep 18 '06 #2
right.options[i]=left.options[j];

First, I apologize not have given an HTML sample with my message.

Paul wrote :
Not really a matter of JavaScript but, of the particular DOM being
manipulated by the javascript. I see the behavior you described in
Firefox but, in IE6 it simply removes from the right list.
What ? You mean it doesn't even perform the affectation ?
What is it that you are actually trying to do ?
You know, two multiple selection lists, with "Add", "Add All",
"Remove", "Remove All" buttons, respectively to add from the first list
to the second, to add all from the first list to the second, to remove
one and remove all with same ways.

The behavior of Firefox allows me to not create options instances
(javascript command new Option()), so I wondered if it was indeed a
normal behavior and not some bug in my source code. And as the target
browser is Firefox, that's cool :-)

Thank you and regards,

--
Mounir

Sep 19 '06 #3

Mounir wrote:
right.options[i]=left.options[j];

First, I apologize not have given an HTML sample with my message.

Paul wrote :
Not really a matter of JavaScript but, of the particular DOM being
manipulated by the javascript. I see the behavior you described in
Firefox but, in IE6 it simply removes from the right list.

What ? You mean it doesn't even perform the affectation ?
What is it that you are actually trying to do ?

You know, two multiple selection lists, with "Add", "Add All",
"Remove", "Remove All" buttons, respectively to add from the first list
to the second, to add all from the first list to the second, to remove
one and remove all with same ways.

The behavior of Firefox allows me to not create options instances
(javascript command new Option()), so I wondered if it was indeed a
normal behavior and not some bug in my source code. And as the target
browser is Firefox, that's cool :-)

Thank you and regards,

--
Mounir
Ah, that is something I can work with :-)
See if this will work for you.

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Select test</title>

<style type="text/css">
#selSwap fieldset{
width: 30em;
height: 10em;
}
#selSwap fieldset div{
display:block;
width: 30%;
float:left;
}
#selSwap fieldset div input, select{
margin-left: 1em;
width: 90%;
font-family: monospace;
}

#selSwap fieldset div input{
height: 1.5em;
margin-top: 0.5em;
}
#selSwap fieldset div select{
height: 10em;
}
</style>
<script type="text/javascript">
function copy(selSrc,sel Dest){
var opts = selSrc.options;
for(var i=opts.length-1; i>=0; i--){
if(opts[i].selected){
selDest.appendC hild(opts[i]);
}
}
}

function copyAll(selSrc, selDest){
var opts = selSrc.options;
for(var i=opts.length-1; i>=0; i--){
selDest.appendC hild(opts[i]);
}
}
</script>

</head>

<body>

<form action="#" id="selSwap">
<fieldset>
<legend>Swapi ng multiselect fun</legend>
<div>
<select multiple="multi ple" name="left" id="left">
<option value="1">Apple </option>
<option value="2">Orang e</option>
<option value="3">Banan a</option>
<option value="4">Grape </option>
</select>
</div>
<div>
<input type="button" value="Add"
onclick="copy(t his.form.left,t his.form.right) " />
<input type="button" value="Add All"
onclick="copyAl l(this.form.lef t,this.form.rig ht)" />
<input type="button" value="Remove"
onclick="copy(t his.form.right, this.form.left) " />
<input type="button" value="Remove All"
onclick="copyAl l(this.form.rig ht,this.form.le ft)" />
</div>
<div>
<select multiple="multi ple" name="right" id="right">
<option value="a">red</option>
<option value="b">orang e</option>
<option value="c">yello w</option>
<option value="d">purpl e</option>
</select>
</div>
</fieldset>
</form>

</body>

</html>

Sep 19 '06 #4
Paul wrote:
<snip><?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<snip>
<input type="button" value="Add"
onclick="copy(t his.form.left,t his.form.right) " />
<snip>

You realise that the common HTML shortcut of referencing named and IDed
elements as named properties of the FORM element is non-standard and
should not be expected to be available in XHTML DOMs?

You should probably be suing DOM standard - this.form.elmen ts.left -
(and similar) if you want to script XHTML DOMs.

Richard.

Sep 19 '06 #5

Richard Cornford wrote:
Paul wrote:
<snip><?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<snip>
<input type="button" value="Add"
onclick="copy(t his.form.left,t his.form.right) " />
<snip>

You realise that the common HTML shortcut of referencing named and IDed
elements as named properties of the FORM element is non-standard and
should not be expected to be available in XHTML DOMs?

You should probably be suing DOM standard - this.form.elmen ts.left -
(and similar) if you want to script XHTML DOMs.

Richard.
True

Sep 19 '06 #6
Paul wrote:
Mounir wrote:
Hi,

Assume that right and left are multiple select elements. It's about the
following line :

right.options[i]=left.options[j];

It copies the content of left.options[j] into right.options[i], but
*removes* the content of the first one !
The "=" character is an assignment operator, it assigns the value of
the right hand side to the value of the left hand side - it isn't a
"copy" operator.

I guess your quandry is to work out what should happen when a reference
to a DOM element is assigned to the value of another DOM element
reference. To take much of the guesswork out of the equation, use DOM
methods (preferably W3C) when DOM elements are involved.

Of course you still need to test widely to ensure browsers are
compliant with whatever standard you have used.

It's hard to google with relevant keywords. Do you know if it's a
normal behaviour of Javascript ?

Not really a matter of JavaScript but, of the particular DOM being
manipulated by the javascript. I see the behavior you described in
Firefox but, in IE6 it simply removes from the right list.
In IE, option elements and DOM just don't like each other, hence the
use of new Option() when creating options rather than createElement. A
DOM version of the above (i.e. to move an option from one select to
another) would be:

right.appendChi ld(left.options[i]);
Which works fine in Firefox & Safari at least, but not IE.
--
Rob

Sep 20 '06 #7
The "=" character is an assignment operator, it assigns the value of
the right hand side to the value of the left hand side - it isn't a
"copy" operator.
Sure, actually i meant "it copies the reference" by "copies the
content", so an assignment.
I guess your quandry is to work out what should happen when a reference
to a DOM element is assigned to the value of another DOM element
reference.
Yes. But, please, do you have an explanation for this behavior ?
To take much of the guesswork out of the equation, use DOM
methods (preferably W3C) when DOM elements are involved.
Thanks. http://www.w3.org/TR/REC-DOM-Level-1/ seems to be a good
starting point.
Of course you still need to test widely to ensure browsers are
compliant with whatever standard you have used.
Yeah, in first time, I will code for firefox (this is an intrant
applications, which assumes that the browser client will be FF).
In IE, option elements and DOM just don't like each other, hence the
use of new Option() when creating options rather than createElement. A
DOM version of the above (i.e. to move an option from one select to
another) would be:
right.appendChi ld(left.options[i]);

Which works fine in Firefox & Safari at least, but not IE.
I've just seen in the previous link a pretty method, in
chapter 2: Document Object Model (HTML) Level 1 :

void add(in HTMLElement element, in HTMLElement before)

It seems to be from a set of specific HTML DOM methods. No problem to
use it, right ?

Regards,

--
Mounir.

Sep 20 '06 #8
Ah, that is something I can work with :-)
See if this will work for you.
Thank you :-)
I hope you haven't passed much time to do.

Interesting line :
selDest.appendC hild(opts[i]);
So appendChild() *removes* the refererence of child from the previous
parent node.

Thanks !

Regards,

--
Mounir

Sep 20 '06 #9
I wrote :
Yes. But, please, do you have an explanation for this behavior ?
Err... in Paul's reply (xhtmI file) I think I got the answer... The
assignment behaves like a DOM child moving (i.e. appendChild()). Right
?

Regards,

--
Mounir

Sep 20 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3370
by: mourad | last post by:
bonjour à tous, j'ai deux questions : 1-j'ai trouvé dans certains logiciels le code suivant : const Maclasse& Maclasse::operator=(const Maclasse&right) { if (&right!=this) { this->Maclasse::~Maclasse(); ::new(this) Maclasse(right); //je ne comprends pas cette ligne.
4
2296
by: Jean-Christophe Michel | last post by:
Hi, In a complex merging of two (non ordered) xml files i need to keep track of the elements of the second tree that were already merged with first tree, to copy only unused elements at the end. I tried two solutions: * first is to 'substract' the used element from existing tree
9
33490
by: Stefan Franke | last post by:
Hi, I've got the following simple XSLT stylesheet, that lists all the values of the elements of any given XML file. <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:for-each select="/"> <xsl:value-of select="current()"/>
6
3403
by: Martin Plantec | last post by:
Hi again, If I may, I have another, slightly more complex, XSLT question. (I'm learning, but it's not as easy as I would have thought!) I would like a rule that says: for every element that has an attribute "webclass", rename the attribute as "class", keeping the same value for the attribute (and keeping the same element name). In other words, I would like to rename an attribute, whatever the element it comes in. I gather it is...
13
3221
by: Noa | last post by:
Hi I have a page that looks like that: <form name="kuku1" action ="anotherpage.html" > <input name="name"> <input name="kuku2"> </form> As far as i know, "getAttribute" should return a string value of an
4
2912
by: patrizio.trinchini | last post by:
Hi all, I'm new to XSLT and maybe my problem have a very trivial answer, but I need an expert that point me in the right direction. What I would obtain is to remove all the elements that have a child element with an attribute set at a given value; maybe an example is more self-explaining... Given the following input XML document:
6
2774
by: Jakob Bieling | last post by:
Hi, I want to move an element from a std::list to the end of the same list. To get this done, I thought I'd just do something like: std::list <intlst; lst.push_back (0); lst.push_back (1); lst.push_back (2); lst.splice (lst.end (), lst, lst.begin (), ++ lst.begin ());
1
5754
by: Tomas | last post by:
When I try to load my xslt i get an xml exception with the message "Root element is missing". The stylesheet works when I preview it in stylus studio, but apparently not in my application. Any ideas what may be wrong? The stylesheet: <?xml version="1.0" encoding="utf-8"?> <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:output indent="yes"/>
2
1937
by: Stephen Durkin | last post by:
I have some javascript to rearrange a list of elements... 1) Each element (div) is initially given a unique id, the same id of the database record the element represents. 2) User "moves" element from slot 3 to slot 6, for example (result: 3- 3) I copy the innerHTML of each div (3-6) into an array. 4) I set the innerHTML of each div (3-6) to now contain the appropriate content from the array. 5) I update the id's of each div (3-6)...
0
8268
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8366
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5575
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2628
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1812
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.