javascript method replace is not working as expected [duplicate] - javascript

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 7 years ago.
My app read DOM object's attribute value. I want this value to be swapped with some new texts and put back to attribute. The original value is:
"position: absolute; background-image: url(\"http://dealer.raymarine.com/Views/Public/ShowImage.ashx?partno=M81203&view=thumb\")";
but when it should be updated with JS replace method, but nothing is changed, why?
Updating JS code:
var styleValue = ui.helper[0].getAttribute("style");
styleValue.replace("raymarine", "XXX");
styleValue.replace("ShowImage", "ShowImageSystemCreator");
styleValue.replace("view=thumb", "view=png");
ui.helper[0].setAttribute("style", styleValue);
console.log("draggable after text swap: " + styleValue);

You're not saving the value anywhere!
styleValue = styleValue.replace("raymarine", "XXX");
styleValue = styleValue.replace("ShowImage", "ShowImageSystemCreator");
styleValue = styleValue.replace("view=thumb", "view=png");
Make it a one-liner if you want by stringing replaces:
styleValue = styleValue.replace("raymarine", "XXX").replace("ShowImage", "ShowImageSystemCreator").replace("view=thumb", "view=png");

Related

CSS styling using Javascript [duplicate]

This question already has answers here:
If I set `let x = document.getElementById("inputText").value` and update `x`, why doesn’t the value update?
(12 answers)
Closed 12 months ago.
Can someone explain why this
x = document.getElementById('bob').style.display;
x = 'hidden';
doesn't work
but
x = document.getElementById('bob');
x.style.display = 'hidden';
works?
So the reason behind this is that in the first example x will return a string value of the display, and changing that will only change the string in you JS code. Whereas in the second example x = to a refrence to and HTML object in the DOM. Changing this variables properties will make a change in the DOM, because it is an HTML element, and not a string.

How to take a value from a link using jquery [duplicate]

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();

Simple Javascript Replace not working [duplicate]

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, " ");

empty jquery element [duplicate]

This question already has answers here:
Getting an empty JQuery object
(4 answers)
Closed 8 years ago.
Is there an elegant way to create an empty jquery element (versus null) ?
$myEmptyElement = $("#ThisIdDoesNotExist234343");
The rational is not checking later on for null.
Later we do :
$myEmptyElement.destroy();
Sure:
var $emptyElement = $();
Why would you want one, though?
A simple example would be:
var $emptyElement = $();
One way:
var $emptyElement = new $;

Replace function not replacing [duplicate]

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");

Categories

Resources