This question already has answers here:
JS replace not working on string [duplicate]
(2 answers)
Closed 5 years ago.
I have dynamically generated buttons with url using data attributes
<button class="btn-Add" type="button" data-add-url="/AddProductToCart?mobileNo=__param__&
productId=2051&quantity=1">Add to Cart</button>
I have the following code:
$(document).ready(function () {
var mobileNo = 0;
$(".btn-Add").click(function () {
mobileNo = $(this).parent('.pro-Group').find('input[type=text]').val();
var postURL = $(this).data('add-url');
postURL.replace('__param__', mobileNo);
alert(postURL); // i can still see __param__ , its not replaced
});
});
Why query string value is not replaced after using replace method ?
Javascript Strings are immutable. So once their value got assigned they can't changes their values.
So you have to reassign them with new changes or have to receive them in new variables. Generally we reassign as there is no need of new variable to be introduced unless you need old value .
postURL = postURL.replace('__param__', mobileNo);
Related
This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 1 year ago.
I have a simple javascript code as follows
var url = '#Url.Action("Action", "Controller", new { id = "_id_", id2 = "_id2_", id3 = "_id3_", id4 = "_id4", ... })';
used in the ajax calls where idxx is replaced with other values
However the var url is generated with "...&id2=id2&..." etc.
and i just learned that javascript string.replace() works only on the first occurance
wondering if there's another way to code the var url rather than replace multiple times ?
the solution has to be universally executable i.e. run in older browsers as well
You can use Replace all which will replace all occurrences
var test = 'id=_id&id=_id&id=test';
var replacedStr = test.replaceAll('_id','teststr');
console.log(replacedStr);
.
This question already has answers here:
How to get URL parameter using jQuery or plain JavaScript?
(34 answers)
Closed 4 years ago.
I have this url
https://myapp.tezze-now.com/sp?id=form&table=user&sys_id=cb6688db0d79b5e9619&view=sp
I need to get text of table name from the url when page loads.
I tried this
location.href.split('&')[1]
It returns "table=user". but I need the table name alone as user.
How can I get this?
Try using URL.searchParams
// location.href
let str = "https://myapp.tezze-now.com/sp?id=form&table=user&sys_id=cb6688db0d79b5e9619&view=sp";
console.log((new URL(str)).searchParams.get("table"));
Split it once more:
location.href.split('&')[1].split('=')[1]
Or you could use Url class (no support in IE):
var url = "https://myapp.tezze-now.com/sp?id=form&table=user&sys_id=cb6688db0d79b5e9619&view=sp"; //window.location.href
var url = new URL(url);
var table = url.searchParams.get("table");
console.log(table);
location.href.split('table=')[1].split('&')[0]
Don't assume the parameter order ;)
This question already has answers here:
remove / replace unwanted prefixed info from `class` name
(2 answers)
Closed 7 years ago.
I am trying to update the style html - by replacing a string, but not working. i am not able to get the final updated html.
here is my try:
var html = "<style>043BF83A8FB24A418DA4248840101D .cls_0 {font:26px 'Arial';}</style><div>testing</div>"
html += $(html).html(function(_,h){
$(h).find('style').html().replace(/^\w+ (\.\w+)/gm,'$1');
});
console.log(html); //throws the error.
jsfiddle
It works for me..
var html = "<style>043BF83A8FB24A418DA4248840101D .cls_0 {font:26px 'Arial';}</style><div>testing</div>";
var $h = $('<div>'+html+'</div>');
$h.find('style').html(function(_,h){ return h.replace(/^\w+ (\.\w+)/gm,'$1'); });
var updated = $h.html();
console.log(updated);
Thanks every one!
This question already has answers here:
Select element by ID from variable [duplicate]
(2 answers)
Closed 9 years ago.
I wanted to just fetch the user defined attribute data using jquery:
Simple code:
var a = 'id';
var patienttype = $a.attr('patientType');
I WANT TO CALL THE RESPECTIVE TEXTBOX ie 'a' and then call its corresponding user defined attribute ie patienttype.
I am not sure of the syntax and couldn't understand how can that be done.
I tried:
$a.attr('patientType'); //undefined
$('#a').attr('patientType') //output:undefined
$("Selector") //selects an DOM object
Let's say that we have an label with id = lbl1 and if we want to add our defined attribute to it we can use the data-attributename
<label id="lbl1" data-custom="aCustomAttr"></label>
Now using the jquery to select the label custom attribute
$("#lbl1").attr("data-custom"); // This will yield aCustomAttr
This question already has answers here:
Get escaped URL parameter
(19 answers)
Closed 9 years ago.
Query string :
Pages/Service.aspx?id=4&comment=1
I am new to jquery ,Kindly help me out with a simple solution to get the Id and COMMENT in "var id & var comment" using jQUERY in asp.net C#
Use following code
<script>
function getQuery(key){
var temp = location.search.match(new RegExp(key + "=(.*?)($|\&)", "i"));
if(!temp) return.
return temp[1];
}
var id = getQuery('id');
var comment = getQuery('comment');
</script>