473,499 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

InnerHTML drops leading tag in SELECT

I am relatively new to XMLRequest, DOM and Ajax but I really like the
functionality. I have the DIV and SPAN changes working well but I
thought that I would experiment with changing the actual innerHTML of
each control rather than changing the whole control inside of a DIV.

What I tried was this:
Create a <SELECTso...

<SELECT id=MyComboBox>
<OPTION>opt1</OPTION>
<OPTION>opt2</OPTION>
<OPTION>opt3</OPTION>
</SELECT>

I then tried to change the innerHTML of just "MyComboBox" but when I
view the resulting string that is supposed to populate the SELECT it
is always missing the first <OPTIONtag. In other words, the string
is supposed to look like this:
strFill = "<OPTION>new1</OPTION><OPTION>new2</OPTION><OPTION>new3</
OPTION>"

....but, instead, it looks like this:
strFill = "new1</OPTION><OPTION>new2</OPTION><OPTION>new3</OPTION>"

I even tried forcing a leading string on the beginning like this...
strResult = "<OPTION>" + strFill;
but it still strips off the leading OPTION tag when I pop the SELECT
(ie. MyComboBox.innerHTML = strResult;)
I checked the innerText property of the result and it looks fine.

Is there something that I am missing or is there an easier way to do
this?
Anyway, until I hear from someone, I guess that it is back to the DIV/
SPAN thing.

Thanks for the help.
Mark

Mar 30 '07 #1
10 2232
mj********@yahoo.com wrote:
I am relatively new to XMLRequest, DOM and Ajax but I really like the
functionality. I have the DIV and SPAN changes working well but I
thought that I would experiment with changing the actual innerHTML of
each control rather than changing the whole control inside of a DIV.

What I tried was this:
Create a <SELECTso...

<SELECT id=MyComboBox>
<OPTION>opt1</OPTION>
<OPTION>opt2</OPTION>
<OPTION>opt3</OPTION>
</SELECT>

I then tried to change the innerHTML of just "MyComboBox" but when I
view the resulting string that is supposed to populate the SELECT it
is always missing the first <OPTIONtag. In other words, the string
is supposed to look like this:
strFill = "<OPTION>new1</OPTION><OPTION>new2</OPTION><OPTION>new3</
OPTION>"

...but, instead, it looks like this:
strFill = "new1</OPTION><OPTION>new2</OPTION><OPTION>new3</OPTION>"

I even tried forcing a leading string on the beginning like this...
strResult = "<OPTION>" + strFill;
but it still strips off the leading OPTION tag when I pop the SELECT
(ie. MyComboBox.innerHTML = strResult;)
I checked the innerText property of the result and it looks fine.

Is there something that I am missing or is there an easier way to do
this?
Anyway, until I hear from someone, I guess that it is back to the DIV/
SPAN thing.

Thanks for the help.
Mark
The select element has an options collection that contains all the
options. Use that to manipulate the options instead.

--
Göran Andersson
_____
http://www.guffa.com
Mar 30 '07 #2
The select element has an options collection that contains all the
options. Use that to manipulate the options instead.
Thanks for the quick reply!
I am trying to keep my javascript to a minimum in order to keep my
pages lightweight. I am mostly using the Jscript for events and Ajax.
I am using ASP.Net (C# scripting) objects on the backend to grab my
data and then populate the controls using the TextWriter classes to
send the HTML back to my pages.
ex:
StringWriter wr = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(wr);
I have not tried your method (but I have thought about it) but my fear
is that the same thing will happen because I will populate the C#
object (my SELECT control) and then send the just the HTML back to the
page. So, there is really no difference. I will try it but I do not
think that it will make a difference.
Any other thoughts?

Thanks again,
Mark
Mar 30 '07 #3
<mj********@yahoo.comwrote in message
news:11**********************@n76g2000hsh.googlegr oups.com...
>The select element has an options collection that contains all the
options. Use that to manipulate the options instead.
Thanks for the quick reply!
I am trying to keep my javascript to a minimum in order to keep my
pages lightweight. I am mostly using the Jscript for events and Ajax.
I am using ASP.Net (C# scripting) objects on the backend to grab my
data and then populate the controls using the TextWriter classes to
send the HTML back to my pages.
ex:
StringWriter wr = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(wr);
Wow! I think you're really making life difficult for yourself!

There's almost never any need to actually write out the HTML markup,
especially for webcontrols...

If the data required to populate your DropDownList is being fetched from a
database, you can simply bind it:

MyDDL.DataSource = <fetch dataset from database>;
MyDDL.DataValueField = "<column from dataset>";
MyDDL.DataTextField = "<column from dataset>";
MyDDL.DataBind();

If not, you can add the select options like this:

MyDDL.Items.Add(new ListItem("OPTION1", "1"));
MyDDL.Items.Add(new ListItem("OPTION2", "2"));
MyDDL.Items.Add(new ListItem("OPTION3", "3"));
Mar 30 '07 #4
Wow! I think you're really making life difficult for yourself!
>
There's almost never any need to actually write out the HTML markup,
especially for webcontrols...

If the data required to populate your DropDownList is being fetched from a
database, you can simply bind it:

MyDDL.DataSource = <fetch dataset from database>;
MyDDL.DataValueField = "<column from dataset>";
MyDDL.DataTextField = "<column from dataset>";
MyDDL.DataBind();

If not, you can add the select options like this:

MyDDL.Items.Add(new ListItem("OPTION1", "1"));
MyDDL.Items.Add(new ListItem("OPTION2", "2"));
MyDDL.Items.Add(new ListItem("OPTION3", "3"));
Yes. That is exactly what I am doing. Sorry, I was try to be brief in
my prior description. But, I only want to update the values (aka
<OPTIONtags) *within* the control, not the entire DIV. Therefore, I
only want to update the internal markup (hence innerHTML) for the
control. I am using the method you are describing above but that
resulting .aspx (or asmx, whichever the case) will update the entire
control within the DIV on my webpage which forces me to resend my
jscript events for the control (ie. onclick=ValidateText();) everytime
I update the control.

Maybe I am missing something. Again, I am relatively new to this.

Thanks for the patience.

Mark

Mar 30 '07 #5
<mj********@yahoo.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
Yes. That is exactly what I am doing. Sorry, I was try to be brief in
my prior description. But, I only want to update the values (aka
<OPTIONtags) *within* the control, not the entire DIV. Therefore, I
only want to update the internal markup (hence innerHTML) for the
control. I am using the method you are describing above but that
resulting .aspx (or asmx, whichever the case) will update the entire
control within the DIV on my webpage which forces me to resend my
jscript events for the control (ie. onclick=ValidateText();) everytime
I update the control.

Maybe I am missing something. Again, I am relatively new to this.
Hmm - do you mean you want to update only a part of the page in isolation
without having to post the entire page back to the server...?

In which case, you need AJAX, as that is precisely the reason for which it
was developed...

Incidentally, since you say you're new to ASP.NET, you might find AJAX a bit
daunting...

In which case, you might prefer to use a relatively lightweight AJAX
wrapper: http://anthemdotnet.com/
Mar 30 '07 #6
That's pretty funny.
Did you read my first post?

Mark

Mar 30 '07 #7
<mj********@yahoo.comwrote in message
news:11*********************@n76g2000hsh.googlegro ups.com...
Did you read my first post?
Not well enough, it seems...

No doubt you've solved your problem now anyway...
Mar 30 '07 #8
Hi,

Incase you are trying to alter the content of your select control in
javascript, you could try this option. Its not the innerHTML that needs to be
set, but its the other html.
<select id="myCombo" >
<option>opt1</option>
<option>opt2</option>
<option>opt3</option>
</select>

To replace the options inside the myCombo select control:

document.getElementById('myCombo').outerHTML = "<select id='myCombo'>

<option>opt4</option>

<option>opt5</option>

<option>opt6</option>

</select>";
I think this shd help.

-Parvathy Padmanabhan

Mar 30 '07 #9
Unfortunately, I did not solve my problem but I am not sure that it is
possible.

Thanks for trying though.

Mar 30 '07 #10
mj********@yahoo.com wrote:
>The select element has an options collection that contains all the
options. Use that to manipulate the options instead.
Thanks for the quick reply!
I am trying to keep my javascript to a minimum in order to keep my
pages lightweight. I am mostly using the Jscript for events and Ajax.
I am using ASP.Net (C# scripting) objects on the backend to grab my
data and then populate the controls using the TextWriter classes to
send the HTML back to my pages.
ex:
StringWriter wr = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(wr);
I have not tried your method (but I have thought about it) but my fear
is that the same thing will happen because I will populate the C#
object (my SELECT control) and then send the just the HTML back to the
page. So, there is really no difference. I will try it but I do not
think that it will make a difference.
Any other thoughts?

Thanks again,
Mark
It's obvious that you haven't tried the method I suggested, as you are
saying that it won't make any difference.

The problem with using innerHTML to replace the elements inside the
select tag, is that there aren't really any elements inside the tag. The
<optiontags get parsed to Option objects that are placed in the
objects collection of the select element.

To add an option to the collection, for example, just create an Option
object and add to the collection:

var o = document.createElement('option');
o.text = 'Select me';
o.value = '42';
document.getElementById('MyComboBox').options.Add( o);

--
Göran Andersson
_____
http://www.guffa.com
Mar 31 '07 #11

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

Similar topics

5
10114
by: Christopher Benson-Manica | last post by:
I'm trying to dynamically change the contents of a select box by doing the following... function myfunc() { var obj=document.getElementById("objname"); // name of the select box var str='';...
3
2563
by: mtz | last post by:
Hi there, It looks like safari has a problem with innerHTML. I have to change a dropdown depending on a selection in another dropdown. When I change the selection in the first dropdown the...
4
8608
by: Michael Schuerig | last post by:
I'm trying to change the options of a select element by setting its innerHTML. Here's a demo case: <html> <head> <script language="javascript" type="text/javascript"> function addOpts() { var...
3
5266
by: soup_or_power | last post by:
Hi Sorry about the heading. I have a table with td consisting of lists with <select></select>. When I do a document.getElementById("element").innerHTML I don't see the selected item. IOW, the...
8
8396
by: McKirahan | last post by:
Firefox does not reflect selected option via innerHTML How do I get Firefox to reflect selected option values? <html> <head> <title>FFinner.htm</title> <script type="text/javascript">...
7
1744
by: MaryA | last post by:
Let me preface this with the fact that I am a newbie to HTML, XML and Javascript. Having said that, let me explain my dilemma: I am having a difficult time getting innerHTML to consistently...
6
7526
by: fmdevelopertim | last post by:
New to AJAX so sorry if this is simple. Have a page that gives the user a list to select a brand. Based on the brand selected it shows a list of sizes available using the following code: ...
0
4102
by: hagar | last post by:
Hi all, I have a problem which I can not understand why this is happening! Debugging this I actually see that it grabs first record then when stepping through code to the line rsImportTo.AddNew...
5
2543
by: pjdeklerk | last post by:
I have a web page which has javascript which uses the innerHTML property of a <TD> object to replace that table cell's contents. The contents are of the form...
0
7131
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,...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7174
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,...
1
6894
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...
0
4600
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...
0
3099
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...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
297
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.