views:

33

answers:

2

I have jQuery code like this workig fine in FF, but totally fails in IE8:

$('#tekst img').each(function(){
                    var scrValue = $(this).attr('src').substring(0,4);
                    var imgTyp = $(this).attr('class').substring(1);
                    if(scrValue == 'tbid')
                    {

                      var splitedString = $(this).attr('src').split(':');
                      var imageID = splitedString[1];
                      $(this).append("<img src='ShowImage.ashx?ID=" + imageID + "' alt= '' />"); 

                    }      
              });

In 'IF' part, IE exits after first (srcValue== true) and even fails to display that first one image.. Any workaround?

A: 

Firstly try alert the image element code to see if its correct, if so try doing the following:

$("<img/>").attr('src','ShowImage.ashx?ID=' + imageID).attr('alt','').appendTo(this);

Might be the way your creating the element.

fir instance your adding a space within the element attribute boundaries alt= '' to alt=''

RobertPitt
Thanks for answer, I had just to avoid img adding to img..To ensure that IE displays images properly I just needed to add parent() after $(this)..
Neno
jQuery has a browser object if you wish to check for IR, such as `if($.browser.ie == true)` and `if($.browser.version >= 7.0)` etc
RobertPitt
+2  A: 

it seem that you are apeending that new image element inside an img element... i dont think this is what you inteneded to do..

if you want to add that img after the image you just selected you could use the after() function in jquery.

or if you want to add the img to your images container you could do something like:

$("#tekst").append("<img src='ShowImage.ashx?ID=" + imageID + "' alt= '' />");

or

$(this).parent().append("<img src='ShowImage.ashx?ID=" + imageID + "' alt= '' />");

instead of:

$(this).append("<img src='ShowImage.ashx?ID=" + imageID + "' alt= '' />");  
guy schaller
Great, that's it.. simple and effective.. Now IE also understands my intentions..
Neno
yea the problem is with ie is that its very very DOM Strict, where as Chrome,Firefox knows your mistakes and fixes them for you, the problem with this is you don't know its a mistake so you do not learn.. +1
RobertPitt