Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set the image src using jQuery
    primarykey
    data
    text
    <p>I am trying to change the image src attribute using jQuery</p> <pre><code>jQuery("#imageID").attr('src','http://localhost:8080/images/1/myImage.png' ); </code></pre> <p>using the above code i can change the src attribute, but when i try this:- </p> <pre><code>jQuery("#imageID").attr('src',jQuery("#imageBlock").css('background-image') ); </code></pre> <p>i am unable to change the src. </p> <p>provided </p> <pre><code>alert ( jQuery("#imageBlock").css('background-image') ); </code></pre> <p>returns:</p> <blockquote> <p>url(<a href="http://localhost:8080/images/1/myImage.png" rel="noreferrer">http://localhost:8080/images/1/myImage.png</a>)</p> </blockquote> <p><strong>Edit #1</strong> Just when i was about to accept the solution. I must say, almost all solution worked in FF. I tried: </p> <ul> <li>slice(4,-1);</li> <li>split("(")[1] > then replace ( ")" , "" );</li> </ul> <p>I guess others will also work. But none of the solutions is working in IE. Reason being: while FF returns: </p> <blockquote> <p>url(<a href="http://localhost:8080/images/1/myImage.png" rel="noreferrer">http://localhost:8080/images/1/myImage.png</a>)</p> </blockquote> <p>IE Returns: </p> <blockquote> <p>url("<a href="http://localhost:8080/images/1/myImage.png" rel="noreferrer">http://localhost:8080/images/1/myImage.png</a>")</p> </blockquote> <p>^^ mind the quotes here</p> <p>Now, what could be the generic way to set the src attr. Do i need to test the browser if it is IE or not?</p> <p>This is the working code.</p> <pre><code>var src = ""; if ( jQuery.browser.msie ) { src = jQuery("#imageBlock").css('background-image').slice(5,-2); }else{ src = jQuery("#imageBlock").css('background-image').slice(4,-1); } jQuery("#imageID").attr('src', src ); </code></pre> <p>I really don't like it :x. If there is another solution than this, then please let me know or else i will accept the <code>slice</code> solution straight away.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload