Change the value of image src in javascript problem
up vote
1
down vote
favorite
Hey guys I'm pretty new to JavaScript.
I want to change the picture dynamically:
HTML:
<div class="col-md-6">
<div class="full-with">
<img src="{{ asset('storage/'. $product->productImg) }}" class="attachment-shop_single wp-post-image" alt="" title="" id="myImg">
</div>
</div>
<div class="summary entry-summary col-md-6">
<h1 itemprop="name" class="product_title entry-title">
{{$product->productName}}
</h1>
<div itemprop="offers" itemscope="" itemtype="http://schema.org/Offer">
<form class="cart" method="post" action="{{ route('cart.add') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="product_size" class="grey">Model</label>
<span class="red">*</span>
<select class="form-control" id="productModel" name="product" onchange="myFunction();">
<option value="">Wybierz model produktu...</option>
@foreach($productModels as $productModel)
<option value="{{$productModel->id}}">{{$productModel->modelName}} - {{$productModel->modelPrice}} @if($productModel->modelPriceCurrency === 1) PLN @else EUR @endif</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="exampleSelect1">Ilość:</label>
<select class="form-control" id="exampleSelect1" name="quantity">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<div class="form-group">
<div itemprop="description">
<p>{!! nl2br(e($productCategory->categoryDescription)) !!}</p>
</div>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Dodaj do wyceny..">
</div>
</form>
</div>
</div>
JS
<script>
function myFunction() {
var model = [
<?php foreach($productModels as $productModel):?>
<?=$productModel?>,
<?endforeach;?>
];
var e = document.getElementById("productModel");
var selectedModelId = e.options[e.selectedIndex].value - 1;
var modelImg = model[selectedModelId].modelImg;
document.getElementById("myImg").src = 'storage/' + modelImg;
}
myFunction();
</script>
The problem is that when I'm changing the source of the picture javascript adds "/item" between: http://127.0.0.1:8000/item/storage/C1pNsNdD6XeJie4RBOp8RjyDocPvcNFGdJJUGfiU.jpeg
Link should look like:
http://127.0.0.1:8000/storage/C1pNsNdD6XeJie4RBOp8RjyDocPvcNFGdJJUGfiU.jpeg
Is there any option to delete "/item"?
javascript html
add a comment |
up vote
1
down vote
favorite
Hey guys I'm pretty new to JavaScript.
I want to change the picture dynamically:
HTML:
<div class="col-md-6">
<div class="full-with">
<img src="{{ asset('storage/'. $product->productImg) }}" class="attachment-shop_single wp-post-image" alt="" title="" id="myImg">
</div>
</div>
<div class="summary entry-summary col-md-6">
<h1 itemprop="name" class="product_title entry-title">
{{$product->productName}}
</h1>
<div itemprop="offers" itemscope="" itemtype="http://schema.org/Offer">
<form class="cart" method="post" action="{{ route('cart.add') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="product_size" class="grey">Model</label>
<span class="red">*</span>
<select class="form-control" id="productModel" name="product" onchange="myFunction();">
<option value="">Wybierz model produktu...</option>
@foreach($productModels as $productModel)
<option value="{{$productModel->id}}">{{$productModel->modelName}} - {{$productModel->modelPrice}} @if($productModel->modelPriceCurrency === 1) PLN @else EUR @endif</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="exampleSelect1">Ilość:</label>
<select class="form-control" id="exampleSelect1" name="quantity">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<div class="form-group">
<div itemprop="description">
<p>{!! nl2br(e($productCategory->categoryDescription)) !!}</p>
</div>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Dodaj do wyceny..">
</div>
</form>
</div>
</div>
JS
<script>
function myFunction() {
var model = [
<?php foreach($productModels as $productModel):?>
<?=$productModel?>,
<?endforeach;?>
];
var e = document.getElementById("productModel");
var selectedModelId = e.options[e.selectedIndex].value - 1;
var modelImg = model[selectedModelId].modelImg;
document.getElementById("myImg").src = 'storage/' + modelImg;
}
myFunction();
</script>
The problem is that when I'm changing the source of the picture javascript adds "/item" between: http://127.0.0.1:8000/item/storage/C1pNsNdD6XeJie4RBOp8RjyDocPvcNFGdJJUGfiU.jpeg
Link should look like:
http://127.0.0.1:8000/storage/C1pNsNdD6XeJie4RBOp8RjyDocPvcNFGdJJUGfiU.jpeg
Is there any option to delete "/item"?
javascript html
Don't add pictures this way, use image icon in editor (these links are broken anyway - 127.0.0.1 is localhost, so we cannot access your pictures)
– barbsan
Nov 11 at 21:56
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Hey guys I'm pretty new to JavaScript.
I want to change the picture dynamically:
HTML:
<div class="col-md-6">
<div class="full-with">
<img src="{{ asset('storage/'. $product->productImg) }}" class="attachment-shop_single wp-post-image" alt="" title="" id="myImg">
</div>
</div>
<div class="summary entry-summary col-md-6">
<h1 itemprop="name" class="product_title entry-title">
{{$product->productName}}
</h1>
<div itemprop="offers" itemscope="" itemtype="http://schema.org/Offer">
<form class="cart" method="post" action="{{ route('cart.add') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="product_size" class="grey">Model</label>
<span class="red">*</span>
<select class="form-control" id="productModel" name="product" onchange="myFunction();">
<option value="">Wybierz model produktu...</option>
@foreach($productModels as $productModel)
<option value="{{$productModel->id}}">{{$productModel->modelName}} - {{$productModel->modelPrice}} @if($productModel->modelPriceCurrency === 1) PLN @else EUR @endif</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="exampleSelect1">Ilość:</label>
<select class="form-control" id="exampleSelect1" name="quantity">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<div class="form-group">
<div itemprop="description">
<p>{!! nl2br(e($productCategory->categoryDescription)) !!}</p>
</div>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Dodaj do wyceny..">
</div>
</form>
</div>
</div>
JS
<script>
function myFunction() {
var model = [
<?php foreach($productModels as $productModel):?>
<?=$productModel?>,
<?endforeach;?>
];
var e = document.getElementById("productModel");
var selectedModelId = e.options[e.selectedIndex].value - 1;
var modelImg = model[selectedModelId].modelImg;
document.getElementById("myImg").src = 'storage/' + modelImg;
}
myFunction();
</script>
The problem is that when I'm changing the source of the picture javascript adds "/item" between: http://127.0.0.1:8000/item/storage/C1pNsNdD6XeJie4RBOp8RjyDocPvcNFGdJJUGfiU.jpeg
Link should look like:
http://127.0.0.1:8000/storage/C1pNsNdD6XeJie4RBOp8RjyDocPvcNFGdJJUGfiU.jpeg
Is there any option to delete "/item"?
javascript html
Hey guys I'm pretty new to JavaScript.
I want to change the picture dynamically:
HTML:
<div class="col-md-6">
<div class="full-with">
<img src="{{ asset('storage/'. $product->productImg) }}" class="attachment-shop_single wp-post-image" alt="" title="" id="myImg">
</div>
</div>
<div class="summary entry-summary col-md-6">
<h1 itemprop="name" class="product_title entry-title">
{{$product->productName}}
</h1>
<div itemprop="offers" itemscope="" itemtype="http://schema.org/Offer">
<form class="cart" method="post" action="{{ route('cart.add') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="product_size" class="grey">Model</label>
<span class="red">*</span>
<select class="form-control" id="productModel" name="product" onchange="myFunction();">
<option value="">Wybierz model produktu...</option>
@foreach($productModels as $productModel)
<option value="{{$productModel->id}}">{{$productModel->modelName}} - {{$productModel->modelPrice}} @if($productModel->modelPriceCurrency === 1) PLN @else EUR @endif</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="exampleSelect1">Ilość:</label>
<select class="form-control" id="exampleSelect1" name="quantity">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<div class="form-group">
<div itemprop="description">
<p>{!! nl2br(e($productCategory->categoryDescription)) !!}</p>
</div>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Dodaj do wyceny..">
</div>
</form>
</div>
</div>
JS
<script>
function myFunction() {
var model = [
<?php foreach($productModels as $productModel):?>
<?=$productModel?>,
<?endforeach;?>
];
var e = document.getElementById("productModel");
var selectedModelId = e.options[e.selectedIndex].value - 1;
var modelImg = model[selectedModelId].modelImg;
document.getElementById("myImg").src = 'storage/' + modelImg;
}
myFunction();
</script>
The problem is that when I'm changing the source of the picture javascript adds "/item" between: http://127.0.0.1:8000/item/storage/C1pNsNdD6XeJie4RBOp8RjyDocPvcNFGdJJUGfiU.jpeg
Link should look like:
http://127.0.0.1:8000/storage/C1pNsNdD6XeJie4RBOp8RjyDocPvcNFGdJJUGfiU.jpeg
Is there any option to delete "/item"?
javascript html
javascript html
edited Nov 11 at 21:52
barbsan
2,14811122
2,14811122
asked Nov 11 at 21:36
Julek Kotruchow
82
82
Don't add pictures this way, use image icon in editor (these links are broken anyway - 127.0.0.1 is localhost, so we cannot access your pictures)
– barbsan
Nov 11 at 21:56
add a comment |
Don't add pictures this way, use image icon in editor (these links are broken anyway - 127.0.0.1 is localhost, so we cannot access your pictures)
– barbsan
Nov 11 at 21:56
Don't add pictures this way, use image icon in editor (these links are broken anyway - 127.0.0.1 is localhost, so we cannot access your pictures)
– barbsan
Nov 11 at 21:56
Don't add pictures this way, use image icon in editor (these links are broken anyway - 127.0.0.1 is localhost, so we cannot access your pictures)
– barbsan
Nov 11 at 21:56
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
To delete /item, do this:
var modelImg = model[selectedModelId].modelImg;
var source = '/storage' + modelImg;
source = source.split("/item");
source.join("");
document.getElementById("myImg").src = source;
1
This answers misses the actual error which is a lacking slash at the start of the path. It "fixes" the problem by adding the slash and then adding a lot of nonsense since "/item" will never occur within the actual string.
– Karl-Johan Sjögren
Nov 11 at 22:20
Code-only answers aren't particularly helpful. You should explain why the OP has their issue and how your code fixes it.
– RobG
Nov 12 at 4:38
add a comment |
up vote
1
down vote
The problem here is simple, you are setting the src attribute to a relative path. storage/ will create a link relative to your current url, so I'm gussing your url is "http://localstorage:8000/storage/something.php" when you get this error.
To make this path absolute from the site root instead you simply prepend it with another slash. That way it will always be relative to your site root no matter what the current url is.
document.getElementById("myImg").src = '/storage/' + modelImg;
The solution by Jack Bashford also adds this slash before the url but then does a lot of nonsense by splitting the string on something that doesn't exist and then joining an array with only one item.
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%2f53253475%2fchange-the-value-of-image-src-in-javascript-problem%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
To delete /item, do this:
var modelImg = model[selectedModelId].modelImg;
var source = '/storage' + modelImg;
source = source.split("/item");
source.join("");
document.getElementById("myImg").src = source;
1
This answers misses the actual error which is a lacking slash at the start of the path. It "fixes" the problem by adding the slash and then adding a lot of nonsense since "/item" will never occur within the actual string.
– Karl-Johan Sjögren
Nov 11 at 22:20
Code-only answers aren't particularly helpful. You should explain why the OP has their issue and how your code fixes it.
– RobG
Nov 12 at 4:38
add a comment |
up vote
0
down vote
accepted
To delete /item, do this:
var modelImg = model[selectedModelId].modelImg;
var source = '/storage' + modelImg;
source = source.split("/item");
source.join("");
document.getElementById("myImg").src = source;
1
This answers misses the actual error which is a lacking slash at the start of the path. It "fixes" the problem by adding the slash and then adding a lot of nonsense since "/item" will never occur within the actual string.
– Karl-Johan Sjögren
Nov 11 at 22:20
Code-only answers aren't particularly helpful. You should explain why the OP has their issue and how your code fixes it.
– RobG
Nov 12 at 4:38
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
To delete /item, do this:
var modelImg = model[selectedModelId].modelImg;
var source = '/storage' + modelImg;
source = source.split("/item");
source.join("");
document.getElementById("myImg").src = source;
To delete /item, do this:
var modelImg = model[selectedModelId].modelImg;
var source = '/storage' + modelImg;
source = source.split("/item");
source.join("");
document.getElementById("myImg").src = source;
answered Nov 11 at 21:40
Jack Bashford
4,59931234
4,59931234
1
This answers misses the actual error which is a lacking slash at the start of the path. It "fixes" the problem by adding the slash and then adding a lot of nonsense since "/item" will never occur within the actual string.
– Karl-Johan Sjögren
Nov 11 at 22:20
Code-only answers aren't particularly helpful. You should explain why the OP has their issue and how your code fixes it.
– RobG
Nov 12 at 4:38
add a comment |
1
This answers misses the actual error which is a lacking slash at the start of the path. It "fixes" the problem by adding the slash and then adding a lot of nonsense since "/item" will never occur within the actual string.
– Karl-Johan Sjögren
Nov 11 at 22:20
Code-only answers aren't particularly helpful. You should explain why the OP has their issue and how your code fixes it.
– RobG
Nov 12 at 4:38
1
1
This answers misses the actual error which is a lacking slash at the start of the path. It "fixes" the problem by adding the slash and then adding a lot of nonsense since "/item" will never occur within the actual string.
– Karl-Johan Sjögren
Nov 11 at 22:20
This answers misses the actual error which is a lacking slash at the start of the path. It "fixes" the problem by adding the slash and then adding a lot of nonsense since "/item" will never occur within the actual string.
– Karl-Johan Sjögren
Nov 11 at 22:20
Code-only answers aren't particularly helpful. You should explain why the OP has their issue and how your code fixes it.
– RobG
Nov 12 at 4:38
Code-only answers aren't particularly helpful. You should explain why the OP has their issue and how your code fixes it.
– RobG
Nov 12 at 4:38
add a comment |
up vote
1
down vote
The problem here is simple, you are setting the src attribute to a relative path. storage/ will create a link relative to your current url, so I'm gussing your url is "http://localstorage:8000/storage/something.php" when you get this error.
To make this path absolute from the site root instead you simply prepend it with another slash. That way it will always be relative to your site root no matter what the current url is.
document.getElementById("myImg").src = '/storage/' + modelImg;
The solution by Jack Bashford also adds this slash before the url but then does a lot of nonsense by splitting the string on something that doesn't exist and then joining an array with only one item.
add a comment |
up vote
1
down vote
The problem here is simple, you are setting the src attribute to a relative path. storage/ will create a link relative to your current url, so I'm gussing your url is "http://localstorage:8000/storage/something.php" when you get this error.
To make this path absolute from the site root instead you simply prepend it with another slash. That way it will always be relative to your site root no matter what the current url is.
document.getElementById("myImg").src = '/storage/' + modelImg;
The solution by Jack Bashford also adds this slash before the url but then does a lot of nonsense by splitting the string on something that doesn't exist and then joining an array with only one item.
add a comment |
up vote
1
down vote
up vote
1
down vote
The problem here is simple, you are setting the src attribute to a relative path. storage/ will create a link relative to your current url, so I'm gussing your url is "http://localstorage:8000/storage/something.php" when you get this error.
To make this path absolute from the site root instead you simply prepend it with another slash. That way it will always be relative to your site root no matter what the current url is.
document.getElementById("myImg").src = '/storage/' + modelImg;
The solution by Jack Bashford also adds this slash before the url but then does a lot of nonsense by splitting the string on something that doesn't exist and then joining an array with only one item.
The problem here is simple, you are setting the src attribute to a relative path. storage/ will create a link relative to your current url, so I'm gussing your url is "http://localstorage:8000/storage/something.php" when you get this error.
To make this path absolute from the site root instead you simply prepend it with another slash. That way it will always be relative to your site root no matter what the current url is.
document.getElementById("myImg").src = '/storage/' + modelImg;
The solution by Jack Bashford also adds this slash before the url but then does a lot of nonsense by splitting the string on something that doesn't exist and then joining an array with only one item.
answered Nov 11 at 22:19
Karl-Johan Sjögren
11k44556
11k44556
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53253475%2fchange-the-value-of-image-src-in-javascript-problem%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
Don't add pictures this way, use image icon in editor (these links are broken anyway - 127.0.0.1 is localhost, so we cannot access your pictures)
– barbsan
Nov 11 at 21:56