Replce img src using Jquery












-2















I am trying to change a part of img src upon hover using jquery (POST with MOBILE). Here is the html code



<a onclick="slideshow(7); return false;"href="http://example.com/12345">
<img src="https://DEF.com/123-post.jpg" width="200" heigh="200" class="fish" id="thumbnail_5">
</a>


and here is the jquery



$('a img').hover (function(){
this.src = this.src.replace('post','mobile');
});


Maybe I am doing something wrong. Please help me out










share|improve this question

























  • try something like. First save the existing src string in a variable, then replace the required string then re-assign the changed string to the src property. Also to get set the property use $(this).prop('src') and $(this).prop('src',myVar) to set the property

    – JustLearning
    Nov 25 '18 at 9:21













  • Why you changed your first code? My code work correctly in snippet but if doesn't work for you check console for error. Also domain DEF.com isn't secure and browser doesn't download image from it that cause hover event doesn't fired for a few seconds.

    – Mohammad
    Nov 25 '18 at 10:32


















-2















I am trying to change a part of img src upon hover using jquery (POST with MOBILE). Here is the html code



<a onclick="slideshow(7); return false;"href="http://example.com/12345">
<img src="https://DEF.com/123-post.jpg" width="200" heigh="200" class="fish" id="thumbnail_5">
</a>


and here is the jquery



$('a img').hover (function(){
this.src = this.src.replace('post','mobile');
});


Maybe I am doing something wrong. Please help me out










share|improve this question

























  • try something like. First save the existing src string in a variable, then replace the required string then re-assign the changed string to the src property. Also to get set the property use $(this).prop('src') and $(this).prop('src',myVar) to set the property

    – JustLearning
    Nov 25 '18 at 9:21













  • Why you changed your first code? My code work correctly in snippet but if doesn't work for you check console for error. Also domain DEF.com isn't secure and browser doesn't download image from it that cause hover event doesn't fired for a few seconds.

    – Mohammad
    Nov 25 '18 at 10:32
















-2












-2








-2


0






I am trying to change a part of img src upon hover using jquery (POST with MOBILE). Here is the html code



<a onclick="slideshow(7); return false;"href="http://example.com/12345">
<img src="https://DEF.com/123-post.jpg" width="200" heigh="200" class="fish" id="thumbnail_5">
</a>


and here is the jquery



$('a img').hover (function(){
this.src = this.src.replace('post','mobile');
});


Maybe I am doing something wrong. Please help me out










share|improve this question
















I am trying to change a part of img src upon hover using jquery (POST with MOBILE). Here is the html code



<a onclick="slideshow(7); return false;"href="http://example.com/12345">
<img src="https://DEF.com/123-post.jpg" width="200" heigh="200" class="fish" id="thumbnail_5">
</a>


and here is the jquery



$('a img').hover (function(){
this.src = this.src.replace('post','mobile');
});


Maybe I am doing something wrong. Please help me out







jquery image replace hover src






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 10:21







SarmadK

















asked Nov 25 '18 at 9:14









SarmadKSarmadK

61110




61110













  • try something like. First save the existing src string in a variable, then replace the required string then re-assign the changed string to the src property. Also to get set the property use $(this).prop('src') and $(this).prop('src',myVar) to set the property

    – JustLearning
    Nov 25 '18 at 9:21













  • Why you changed your first code? My code work correctly in snippet but if doesn't work for you check console for error. Also domain DEF.com isn't secure and browser doesn't download image from it that cause hover event doesn't fired for a few seconds.

    – Mohammad
    Nov 25 '18 at 10:32





















  • try something like. First save the existing src string in a variable, then replace the required string then re-assign the changed string to the src property. Also to get set the property use $(this).prop('src') and $(this).prop('src',myVar) to set the property

    – JustLearning
    Nov 25 '18 at 9:21













  • Why you changed your first code? My code work correctly in snippet but if doesn't work for you check console for error. Also domain DEF.com isn't secure and browser doesn't download image from it that cause hover event doesn't fired for a few seconds.

    – Mohammad
    Nov 25 '18 at 10:32



















try something like. First save the existing src string in a variable, then replace the required string then re-assign the changed string to the src property. Also to get set the property use $(this).prop('src') and $(this).prop('src',myVar) to set the property

– JustLearning
Nov 25 '18 at 9:21







try something like. First save the existing src string in a variable, then replace the required string then re-assign the changed string to the src property. Also to get set the property use $(this).prop('src') and $(this).prop('src',myVar) to set the property

– JustLearning
Nov 25 '18 at 9:21















Why you changed your first code? My code work correctly in snippet but if doesn't work for you check console for error. Also domain DEF.com isn't secure and browser doesn't download image from it that cause hover event doesn't fired for a few seconds.

– Mohammad
Nov 25 '18 at 10:32







Why you changed your first code? My code work correctly in snippet but if doesn't work for you check console for error. Also domain DEF.com isn't secure and browser doesn't download image from it that cause hover event doesn't fired for a few seconds.

– Mohammad
Nov 25 '18 at 10:32














1 Answer
1






active

oldest

votes


















0














You should put new string in second parameter of String.replace() instead of using it twice.



$('a img').hover(function(){
this.src = this.src.replace('post', 'mobile');
});





$('a img').hover(function(){
this.src = this.src.replace('post', 'mobile');
console.log(this.src);
});

img {
width: 100px;
height: 100px;
border: 1px solid red;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="http://example.com/12345">
<img src="http://aDEF.com/123-post.jpg" width="200" heigh="200" class="fish" id="thumbnail_5">
</a>








share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466114%2freplce-img-src-using-jquery%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You should put new string in second parameter of String.replace() instead of using it twice.



    $('a img').hover(function(){
    this.src = this.src.replace('post', 'mobile');
    });





    $('a img').hover(function(){
    this.src = this.src.replace('post', 'mobile');
    console.log(this.src);
    });

    img {
    width: 100px;
    height: 100px;
    border: 1px solid red;
    }

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <a href="http://example.com/12345">
    <img src="http://aDEF.com/123-post.jpg" width="200" heigh="200" class="fish" id="thumbnail_5">
    </a>








    share|improve this answer




























      0














      You should put new string in second parameter of String.replace() instead of using it twice.



      $('a img').hover(function(){
      this.src = this.src.replace('post', 'mobile');
      });





      $('a img').hover(function(){
      this.src = this.src.replace('post', 'mobile');
      console.log(this.src);
      });

      img {
      width: 100px;
      height: 100px;
      border: 1px solid red;
      }

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <a href="http://example.com/12345">
      <img src="http://aDEF.com/123-post.jpg" width="200" heigh="200" class="fish" id="thumbnail_5">
      </a>








      share|improve this answer


























        0












        0








        0







        You should put new string in second parameter of String.replace() instead of using it twice.



        $('a img').hover(function(){
        this.src = this.src.replace('post', 'mobile');
        });





        $('a img').hover(function(){
        this.src = this.src.replace('post', 'mobile');
        console.log(this.src);
        });

        img {
        width: 100px;
        height: 100px;
        border: 1px solid red;
        }

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <a href="http://example.com/12345">
        <img src="http://aDEF.com/123-post.jpg" width="200" heigh="200" class="fish" id="thumbnail_5">
        </a>








        share|improve this answer













        You should put new string in second parameter of String.replace() instead of using it twice.



        $('a img').hover(function(){
        this.src = this.src.replace('post', 'mobile');
        });





        $('a img').hover(function(){
        this.src = this.src.replace('post', 'mobile');
        console.log(this.src);
        });

        img {
        width: 100px;
        height: 100px;
        border: 1px solid red;
        }

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <a href="http://example.com/12345">
        <img src="http://aDEF.com/123-post.jpg" width="200" heigh="200" class="fish" id="thumbnail_5">
        </a>








        $('a img').hover(function(){
        this.src = this.src.replace('post', 'mobile');
        console.log(this.src);
        });

        img {
        width: 100px;
        height: 100px;
        border: 1px solid red;
        }

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <a href="http://example.com/12345">
        <img src="http://aDEF.com/123-post.jpg" width="200" heigh="200" class="fish" id="thumbnail_5">
        </a>





        $('a img').hover(function(){
        this.src = this.src.replace('post', 'mobile');
        console.log(this.src);
        });

        img {
        width: 100px;
        height: 100px;
        border: 1px solid red;
        }

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <a href="http://example.com/12345">
        <img src="http://aDEF.com/123-post.jpg" width="200" heigh="200" class="fish" id="thumbnail_5">
        </a>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 9:57









        MohammadMohammad

        15.5k123461




        15.5k123461






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466114%2freplce-img-src-using-jquery%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks