Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 18th, 2004, 09:40 AM
Authorized User
 
Join Date: May 2004
Location: , , .
Posts: 28
Thanks: 0
Thanked 1 Time in 1 Post
Default Javascript to HTML Special Characters

We have a page which has some dynamic combo boxes which are initially populated data from a database which contain HTML special characters such as uml and acutes. However as they are dynamic various parts of the page populate the combo box with a refreshed list via a Javascript Array. However this then shows the special characters literally i.e. é rather than the actual character.

how can we get round this. here is a small sample page to show our problem.

<html>

    <head>
        <script>
            test = new Array;
            test[0]=new Array("T&auml;bert","test");

            test2 = "T&auml;ubert";

            function setVal1()
            {
                document.gui.aSelect.options[0].text = "1 - " + test[0][0];
            }

            function setVal2()
            {
                document.gui.aSelect.options[0].text = "2 - T&auml;ubert";
                escapeXml="false"
            }
        </script>
    </head>

    <body>

        <FORM NAME="gui">
            <SELECT NAME="aSelect">
                <OPTION VALUE="deafert">Taubert
            </SELECT>
            <P>
            <INPUT TYPE="button" VALUE="Copy from array in JS" onclick="setVal1()">
            <P>
            <INPUT TYPE="button" VALUE="Set explicitly from JS function" onclick="setVal2()">
            <P>
            <INPUT TYPE="button" VALUE="Set explicitly from HTML" onclick="document.gui.aSelect.options[0].text = '3 - T&auml;ubert'">
            <P>
            <INPUT TYPE="button" VALUE="Copy from JS array explicitly from HTML" onclick="document.gui.aSelect.options[0].text = '4 - ' + test[0][0]">
            <P>
            <INPUT TYPE="button" VALUE="Copy from JS variable explicitly from HTML" onclick="document.gui.aSelect.options[0].text = '5 - ' + test2">
        </FORM>
    </body>

</html>

Reply With Quote
  #2 (permalink)  
Old May 18th, 2004, 11:38 PM
Friend of Wrox
Points: 3,558, Level: 25
Points: 3,558, Level: 25 Points: 3,558, Level: 25 Points: 3,558, Level: 25
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: California, USA
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

Sorry but I couldnt get ur question! its fine in my opinian since it show what evr u set for?! what do u want to be displayed?!

Kepp in touch.

Always:),
Hovik Melkomian.
Reply With Quote
  #3 (permalink)  
Old May 19th, 2004, 02:49 AM
Authorized User
 
Join Date: May 2004
Location: , , .
Posts: 28
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hovik

Thanks for the reply.

What I want is to display the a uml character. If you run the code I provided press the 3rd button "Set Explicitly from HTML" then look in the box thats what I want, an a with 2 dots over it which is an uml used in european languages.

In HTML this is done by using &auml; but in javascript its /344. The problem is that when the text is passed out its not converting it into the HTML equivalent.

You will notice in the code that I'm using the same string in all examples of the button but only the 3rd will display the correct format. I need a way to do this for all of the buttons in this example or use so sort of Javascript function to return the pure html code.

Cheers

Andy Gilfrin...



Reply With Quote
  #4 (permalink)  
Old May 19th, 2004, 03:28 AM
Friend of Wrox
 
Join Date: Jul 2003
Location: , , United Kingdom.
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Andy,

The reason the third method works is that entities in the onclick attribute of the button are parsed when the page is rendered.

One (dirty) way to do what you want would be to create a hidden element in your page e.g.

<div id="Parser" style="display:none;"></div>

and populate it using innerHTML, causing the entities to be parsed then copy the innerHTML into the text property of the option element...

function SetVal(){
    var d = document;
    var p = d.getElementById("Parser");
    p.innerHTML = "2 - T&auml;ubert";
    d.gui.aSelect.options[0].text = p.innerHTML;
}

HTH,

Chris

Reply With Quote
  #5 (permalink)  
Old May 19th, 2004, 04:33 AM
Authorized User
 
Join Date: May 2004
Location: , , .
Posts: 28
Thanks: 0
Thanked 1 Time in 1 Post
Default

Thanks Chris.

Thank works a treat, but I'm not sure if thats going to be suitable as we are currently using a Javascript array to populate the values.

The main problem is that its not just one page way might have this problem on it could be lots of pages. But thanks for the info and it gives me a starting point to go back to the team to see what we can do.

Maybe we could build a function to get the data from the array and then return that using the method you described. I'll have to look at the code to see how it gets the data from the array.

Thanks alot.

Andy Gilfrin

:)

Reply With Quote
  #6 (permalink)  
Old May 19th, 2004, 05:01 AM
Friend of Wrox
 
Join Date: Jul 2003
Location: , , United Kingdom.
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Andy,

One way to tackle it might be to create a form in your page (server-side from the database) that contains hidden fields:

<form id="ArraySrc">
    <input type="hidden" value="1 - &quot;value&quot;" />
    <input type="hidden" value="T&auml;ubert" />

</form>

Then use it to populate your array when the page loads (call LoadArray() from the body onload)...

var gArray;
function LoadArray(){
    gArray = new Array();
    var src = document.forms["ArraySrc"];
    for(var i = 0; i < src.elements.length; i++){
        gArray[i] = src.elements[i].value;
    }
}

Then just access the array as normal.

Best regards,

Chris



Reply With Quote
  #7 (permalink)  
Old May 19th, 2004, 09:25 AM
Authorized User
 
Join Date: May 2004
Location: , , .
Posts: 28
Thanks: 0
Thanked 1 Time in 1 Post
Default

Thanks for the follow up Chris.

I actually managed to get it working simply using the method you described. I created a function as showed then called that passing in the array reference and then passing out the HTML values.

I works fine so thanks once again.

Andy Gilfrin...

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
New help with Pattern for special characters 2BOrNot2B Java Basics 1 July 2nd, 2008 04:17 PM
Inserting Special Characters From JavaScript richard.york Javascript 5 June 9th, 2005 01:04 PM
storing special special characters in nvarchar... ACE2084 SQL Server 2000 2 February 9th, 2005 11:45 AM
special characters lian_a Classic ASP Basics 3 June 23rd, 2004 05:16 AM



All times are GMT -4. The time now is 03:07 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.