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

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

Related

Add double quotes to arraylist of string in javascript [duplicate]

This question already has answers here:
How to split comma separated string using JavaScript? [duplicate]
(4 answers)
Closed 3 years ago.
I am new to stack overflow learning javascript and programming.
I have a problem that i am stuck while learning and thinking any help on this question will be
useful for me thanks and the question is:
Example i have a variable a in the code below and i want to convert it to an array in javascript
var a = ["baby,cat,dog"]
i wanted it to be
a = ["baby","cat","dog"].
Use String.prototype.split() as below
var a = ["baby,cat,dog"];
a = a[0].split(',');
console.log(a);
You could take the array and map the splitted values and get a flat array back.
var array = ["baby,cat,dog"];
result = array.flatMap(s => s.split(','));
console.log(result);

serialized string to form [duplicate]

This question already has answers here:
How to convert URL parameters to a JavaScript object? [duplicate]
(34 answers)
Closed 3 years ago.
I have serialized form in string such as:
"name=Michael&surname=Davies&multiple=selection1&multiple=selection3"
How can I convert this string to object and then change values in form, or directly change values in form with this string? I have several forms in one html page and each string represent values in one form.
Thank you.
Try using below to get an array. Then u can put them in to a JSON object by traversing through the array. May be not the best solution.
$("#form").serializeArray();

Get the query string value from a URL using javascript [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 6 years ago.
I have a URL like
http://something.com?acess_token=ashv6786sdjfhjd
When I click on that link, how to get the access_token from that link using Javascript?
Try :
var Yoururl= "http://something.com?acess_token=ashv6786sdjfhjd";
var acessToken=Yoururl.split('?')[1].split('=')[1];
Working Fiddle
For url parameter see old so question from given link.
Get url parameter via jquery
If you want the value of the access_token you mean this:
ashv6786sdjfhjd
Then here's a quick solution. Hope it helps!
var str = "http://something.com?acess_token=ashv6786sdjfhjd.when";
var accessToken = str.split("=")[1].split(".")[0];
console.log(accessToken);

Javascript replace not working with ampersand and defining a letter [duplicate]

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

.find('.foo').text() as array [duplicate]

This question already has answers here:
Get array of text values using selector instead of iterating?
(7 answers)
Closed 9 years ago.
I have html with multiple things classed .text. I the perform
console.log($(whatever).find('.foo').text());
The result is a textstring with all the contents in it. Can I save it to an array instead somehow?
You can use the map method to get this done
var textArray = $(whatever).find('.foo').map(function() {
return $(this).text();
}).get();
This will get you an array.. If you want to display it as a string then just use join along with it.
Check Fiddle
var arr = [];
$(whatever).find('.foo').each(function() {
arr.push($(this).text());
});

Categories

Resources