1

I have a website and based on language en, sq or el, I want to change image source. So I have a folder named screenshots then there are three folders: en, sq and el.

For example the URL for a picture named 'slide-phone.png' is:

static/img/screenshots/en/slide-phone.png
static/img/screenshots/sq/slide-phone.png
static/img/screenshots/el/slide-phone.png

I want to take attribute lang of HTML tag and then to add it to the source of the image, but for some reasons, it's not working.

My code looks like this:

<html lang="{{ _('en') }}">
<script type="text/javascript">
 $(document).ready(function(e){
       var language = $('html').attr('lang');
});
</script>
...
 <img  id="slide-phone-1-img" src="{{ url_for('static', filename='img/screenshots/' + language+'slide-phone.png') }}" >

I'm using python and jinja. Any help :)

EDIT:

I also tried like this but the problem is that I want to use jinja and for that I need to change this code:

<html lang="{{ _('en') }}">
    <script type="text/javascript">
     $(document).ready(function(e){
           var language = $('html').attr('lang')
           img_url = 'img/screenshots/'+ language+'/slide-phone.png';
           $('#slide-phone-img').attr('src',img_url);
    });
    </script>
5
  • how should it work ? How and where you call your variable language ? Commented Mar 2, 2016 at 10:09
  • @MKAD inside the src in image div ? Commented Mar 2, 2016 at 10:10
  • you cant call your language variable out of <script type="text/javascript"> tags Commented Mar 2, 2016 at 10:15
  • is any way to do it ? Commented Mar 2, 2016 at 10:16
  • let me know If it does not work Commented Mar 2, 2016 at 10:30

1 Answer 1

1

something like this

$(document).ready(function(e){
       var language = $('html').attr('lang');
       var new_url = $('#slide-phone-1-img').attr("src").replace("language", language);
       console.log(new_url);
       $('#slide-phone-1-img').remove();
       $('#imges').prepend('<img id="slide-phone-1-img" src="'+new_url+'" />');

$('#show').html( $('#slide-phone-1-img').attr("src").replace("language", language));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html lang="{{ _('en') }}">
   <body>
    <div id="imges">
     <img  id="slide-phone-1-img" src="{{ url_for('static', filename='img/screenshots/language slide-phone.png') }}" > 
    </div>   
    <div id="show">
      <p></p>
    </div>
   </body>
</html>

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

9 Comments

I did a console.log and it shows the good link like this :/static/img/screenshots/en/slide-phone.png , but in the scr is not replaced. Is this maybe because page loads and after it the jquery code is executed?
youe mean the src in your html img tag is not replaced?
if I understood you correctly, you want to change your code in your example.html after jQuery once? from <img id="slide-phone-1-img" src="{{ url_for('static', filename='img/screenshots/language slide-phone.png') }}" > ..... to <img id="slide-phone-1-img" src="{{ url_for('static', filename='img/screenshots/{{ _('en') }} slide-phone.png') }}" > right?
Yes exactly,ur code works as I said before If I do a console.log($('#slide-phone-1-img').attr("src").replace("language", language)) it gives the right url, but the url in src is not changed .
|

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.