Replce img src using Jquery
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
add a comment |
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
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 domainDEF.com
isn't secure and browser doesn't download image from it that causehover
event doesn't fired for a few seconds.
– Mohammad
Nov 25 '18 at 10:32
add a comment |
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
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
jquery image replace hover src
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 domainDEF.com
isn't secure and browser doesn't download image from it that causehover
event doesn't fired for a few seconds.
– Mohammad
Nov 25 '18 at 10:32
add a comment |
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 domainDEF.com
isn't secure and browser doesn't download image from it that causehover
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
add a comment |
1 Answer
1
active
oldest
votes
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>
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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>
add a comment |
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>
add a comment |
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>
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>
answered Nov 25 '18 at 9:57
MohammadMohammad
15.5k123461
15.5k123461
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 causehover
event doesn't fired for a few seconds.– Mohammad
Nov 25 '18 at 10:32