Why can I not find using data attribute [duplicate] - javascript

This question already has answers here:
jQuery Data vs Attr?
(3 answers)
Closed 5 years ago.
In code I do this:
var markerName = $(fileInput).closest('tr.file-input-row').find('input[type="text"]')[0].value.replace(/[^a-z0-9]/gi, '-');
$.data(fileInput, 'for', markerName);
in this case, markerName is "file-1"
If I check using:
$('input[type="file"][data-for="file-1"]')
I get an object with length equal to 0... so not found.
However, if I do:
$('input[type="file"]:first').data().for
in this case, the first input[type="file"] is the same input I set the data attribute for, I get:
"file-1"
...as expected.
It looks like it is being set, but it is then not accessible.
Any thoughts why?
TIA

This is not working because you aren't updating the DOM object when you perform the data-for adjustment in the first part of your code. To update the DOM object you should use attr(key, value).
For more info on the differences between data and attr there is a good answer related to this: jQuery Data vs Attr?

If I check using:
$('input[type="file"][data-for="file-1"]')
Neither .data() nor jQuery.data() is part of the object returned by jQuery() : $() call.

Related

How to use custom variable in JavaScript [duplicate]

This question already has answers here:
Javascript getElementById based on a partial string
(9 answers)
Closed 3 years ago.
I just want to know to how to use custom varible with javascript.suppose we have 5 id with name zawsz,baws2,tawsx,paws4,vawsa and we want to execute single DOM command to all these element whose id is define here.
document.getElementById("-aws-").onclick=function(){}
Here -aws- define all the id define above.(-) can be replace with any char/int value;
You could use the following code:
(The following code will select all elements of which the id includes aws.
I have tested this code and it works: https://jsfiddle.net/5042woqz/)
document.querySelectorAll('*[id*="aws"]').forEach(function(item) {
item.onclick=function() {
console.log('click!');
};
});
Items will now be an array containing all your aws- items.
If you have further questions, just let me know.
P.S.: You could achieve the same thing really easily with jquery.
You can use document.querySelectorAll for this:
document.querySelectorAll('[id^="aws"]')
That will select all elements where the id attribute starts with (^=) "aws".

Retrieving parameter from query string based on another string in Javascript [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 4 years ago.
My app in Google Apps Script gets data from a form that I want to process.
The element form is received and contains the different parameters and values.
If I log the element form it looks like {uid=11, tradingname=xxx, email=yyy}
I can pick up the separate values by running form.uid or form.tradingname for example. But what I want is to pick up the parameters from the element form by referring to a String "uid" (because I get this value form the headers row of the spreadsheet).
Is there a simple way I can do this in one line, for example something like:
form.element["uid"]
So far I've only found difficult methods like https://gomakethings.com/how-to-get-the-value-of-a-querystring-with-native-javascript/ and I would think that I should be able to do this in an easier way.
Thanks!
You can use $("#yourform").serializeArray() and then iterate to match "name" values you want

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

Jquery selector performance & behavior [duplicate]

This question already has answers here:
Performance of jQuery selectors vs local variables
(5 answers)
Closed 7 years ago.
I have a strange question about jquery selector behavior.
First approach:
$('#div').find('#something').html('hahah');
$('#div').find('#something').html('hahah');
$('#div').show();
Second approach:
var $div = $('#div');
$div.find('#something').html('hahah');
$div.find('#something').html('hahah');
$div.show();
I know that it might not have too much difference, but is the second faster than the first?? I've always used the second approach but I'm not sure if there is a difference because I don't know how the jquery selector algorithm works.
The second way is faster/better because you have cached the selector.
Everytime you call $('selector'), the jQuery selector engine(sizzle) is called to locate your desired elements.
However, when you store them in a variable, you do not need to repeatedly call the selector engine as the results are stored.
Note, in your example above, caching can be further improved by storing the find() result as well
var $something = $('#div').find('#something');
$something.html('hahah');
$something.html('hahah');
$something.show();

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

Categories

Resources