1

Sorry if it's a noob question !

I'm using a script to translate my page with this code:

<script>
        var translations= { 'en' : 
                                {'title' : 'Title', 'textimg' : 'English text'},
                            'fr' : 
                                {'title' : 'Titre', 'textimg' : 'Texte français'}
                          };
        function doTranslate(language) {
            for(id in translations[language]) {
                document.getElementById(id).innerHTML = translations[language][id];
            }
        }
</script>

And this html:

<a href="javascript:doTranslate('fr')"><img src="img/Fr-Flag.png"></a>
<a  href="javascript:doTranslate('en')"><img src="img/UK-Flag.png"></a>
<h2 id="title">Title</h2>

The problem comes when I use an image (little icon): the text changes, but src seems to disapear, so when the text change, the image is not displayed:

<img id="textimg" src="img/fav-rond.png">English text</img>

How to solve this ?

4
  • What is your expected output? Something like English [x] where English is some arbitrary translated text and [x] is the flag image? Commented Apr 6, 2016 at 9:27
  • I would like to have the image beside the changed text, like this: image English text -> image Texte français Commented Apr 6, 2016 at 9:29
  • which text is changing and which id you are passing? Commented Apr 6, 2016 at 9:31
  • Maybe you should provide a more complete HTML of your current implementation, it seems a bit ambiguous now. Commented Apr 6, 2016 at 9:42

5 Answers 5

2

Please see this answer about content inside the img tags: Div tag within img tag

But a possible solution is to remove the id from img and create a new element around the text with that id. For example:

<img src="img/fav-rond.png" />
<span id="textimg">English text</span>

And a demo: https://jsfiddle.net/7j2ckw0r/1/

Sign up to request clarification or add additional context in comments.

2 Comments

I have tried this, but the image is not beside the text
Make it span or use a little CSS
0

This should solve your issue.

document.getElementById(id).setAttribute("id", translations[language][id])

You shouldn't be using innerHTML to solve this issue.

1 Comment

@SGS Venkatesh Your solution helped me to solve another problem (href) ! ;)
0

The <img> tag does not allow any information within, it is a "Empty element"

If you add any text inside, that becomes an invalid HTML text.

What you can do is adding an alt attribute:

<img id="textimg" src="img/fav-rond.png" alt="English text"/>

Comments

0

I strongly recommend you to visit this link, you will find the optimal way to translate your html page in pure JavaScript code.

JQuery search dom elements just after rendering and replace keys by its corresponding values

Comments

0

It isn't possible to add text to an image tag, instead place a span element directly after the image and access that instead.

<img src="img/fav-rond.png" /><span id="textimg">English text</span>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.