This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 8 years ago.
I followed some documentation to use the JavaScript replace function and it's not changing anything. No errors are thrown. Any idea what I'm doing wrong? The variable is retrieved from XML - maybe it needs to be cast as a string or something?
for (var i = 0, iln = projects.length; i < iln; i++){
var thumb = projects[i].get('thumb');
thumb.replace("200.jpg", "640.jpg");
console.log(thumb) //200.jpg not replaced
}
The full thumb value should look like this:
http://b.vimeocdn.com/ts/160/895/160895498_200.jpg
Is there a better way to find and replace things?
Assign the value back into thumb.
thumb = thumb.replace("200.jpg", "640.jpg");
Try:
thumb = thumb.replace("200.jpg", "640.jpg");
Related
This question already has answers here:
ES6 template literals vs. concatenated strings
(4 answers)
Closed 1 year ago.
I'm start coding.
While I was watching a lecture, I saw code like this.
var coworkers = ['go', 'hello', 'hi', 'doit'];
<script type="text/javascript">
var i = 0;
while(i < coworkers.length){
document.write('<li>'+coworkers[i]+'</li>');
i = i + 1;
}
But But when I searched, Said to use ${variable} in JavaScript. and It didn't work.
How are +variable+ and ${variable} different in javascript? Thanks :)
Those placeholders, like ${variable}, can only be used in template literals, which are always enclosed in backticks(the key right below you escape key).
If it still doesn’t work, maybe you’re using an older browser?
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
I'd like to take some user input and use it to find a certain object in an array. But when I try to do it with the code below, I get an undefined error. What am I doing wrong?
function findNextLevel() {
var currentLevel = parseFloat(document.getElementById("currentLevel").value);
var xpForLevel = trainerLevels.currentLevel;
document.getElementById("result01").innerHTML = xpForLevel;
}
I'm assuming that trainerLevels is an array and currentLevel is an index. If so, the way to access an element in an array at a certain index is to use brackets like so. Otherwise, could you provide more details in your question?
var xpForLevel = trainerLevels[currentLevel];
If this is the answer that you were looking for, then may I recommend that you use the parseInt rather than the parseFloat function for getting the index? And also, since it is user input, you may want to check that currentLevel is in the correct range as well.
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 7 years ago.
I want to pass one value through ajax by taking the values from jQuery. But I am using link so I have problems taking the value. I tried the following,
<a id="addpa" class="ActionPopup" href="http://localhost:49951/admin/assignhome/Add?sPId=7">Add</a>
Jquery Code:
var spId = $("#addpa").prop("href"); // Here i am getting a whole Url
var thequerystring = getParameterByName("sPId");
The result is showing undefined. How to take the value of sPId? Give me ideas..
How to take the value of sPId?
Try using String.prototype.split() , Array.prototype.pop()
var spId = $("#addpa").prop("href").split(/=/).pop();
This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 8 years ago.
I have written a JSFiddle with the expected output and output my code is currently doing. The two different values must be parsed as either a colon or a semi-colon as I need to know what one line to parse in php is.
var data = "key=update.repositories&value=xime+mcsg+mcsg-maps&key=server.minPlayersToStart&value=12";
data.replace(/&v/g, ":v");
data.replace(/&k/g, ";k");
$(".encData").text(data);
Fiddle found here: http://jsfiddle.net/RS6xC/1/
string.replace() doesn't change the original variable, it returns a new value.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
So you need to reassign the returned value of the replace() method to the original variable, such as like this:
var data = "key=update.repositories&value=xime+mcsg+mcsg-maps&key=server.minPlayersToStart&value=12";
data = data.replace(/&v/g, ":v");
data = data.replace(/&k/g, ";k");
$(".encData").text(data);
.replace returns a new string, it does not modify the existing string.
Use:
data=data.replace(/&v/g, ":v");
data=data.replace(/&k/g, ";k");
This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 3 years ago.
This seems so simple and trivial but it is not working. Here is my javascript:
var url = "/computers/";
console.log(url);
url.replace(/\//gi, " ");
console.log(url);
And here is the output in my browsers console:
/computers/
/computers/
As you can see nothing changes. As you can tell from the code I'm trying to replace the forward slashes with spaces. What am I doing wrong?
url = url.replace(/\//gi, " ");
Nothing changes because you're not assigning the result of the replacement to a variable. Add url = url.replace()
url.replace(/\//gi, " "); returns the resulting string (in javascript you can't modify an existing string), you are not assigning it to anything
assign it like so:
url = url.replace(/\//gi, " ");