Jquery selecting element by 'data-id' [duplicate] - javascript

This question already has answers here:
jQuery how to find an element based on a data-attribute value?
(9 answers)
Closed 8 years ago.
Similar to the question here , I need to get an element based on its data-id.
The problem I encounter is that I want to retrieve a an a element with a certain data-id using a variable.
lets consider the code below:
var item_id = 12345;
if I want to get the element that has the data-id with the value of item_id, would I do the following ?
$('[data-id=item_id]')
This does not work, and I am kind of stuck on how I can get around this.

You can concatenate the variable using +:
$('[data-id=' + item_id + ']')

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".

Use value of id to search for class [duplicate]

This question already has answers here:
How to use JavaScript variables in jQuery selectors?
(6 answers)
Closed 4 years ago.
Im trying to get the value of an html element via Id. I now want to use that value to loop through all the elements with the class from the id.val. How can I search for a variable as a class.
$(".BtnSettings").click(function () {
var Daytime = $("#Daytime").val();
//Here i dont know what to do. Instead of .Daytime I want to search for the var Daytime. Or for $("#Daytime").val() obviously, doesnt matter.
$('.Daytime').each(function () {
$(this).prop("checked", true); // Element(s) are now enabled.
});
})
you can use the attr method of jquery to look for a specific attribute just set a value. if you want to change value of said attribute the second parameter of the function will set it for you like.
Daytime.attr("class","newClass")
$("#Daytime").attr("class")
will give you the class of #Daytime

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

Destroy DIVs with their IDs containing a particular string [duplicate]

This question already has answers here:
How to find if div with specific id exists in jQuery?
(10 answers)
Closed 7 years ago.
I have some DIVs with IDs like 'abc_chosen', 'danger_chosen', etc.
I would like to delete all DIVs when their IDs contain the string '_chosen'.
How can I do that?
Try this:
$('div[id$="_chosen"]').remove();
$ here will check if _chosen is at the end of id.
Thanks to #evolutionxbox:
If you want to check if id contain _chosen anywhere(not at the end)
$('[id*="_chosen"]').remove();
Docs: https://api.jquery.com/attribute-ends-with-selector/

jquery - how to add an variable as an id in jquery command.? [duplicate]

This question already has answers here:
How to use JavaScript variables in jQuery selectors?
(6 answers)
Closed 8 years ago.
Its a simple question, could not find an answer from google.
Code
$.click(function(){
var curID = $(this).parent()[0].id;
$("#"+curID input).attr("checked",true);
});
Onclick function is giving me the parent id, Now, using the parent id i want to find the input element and add checked attribute.
I am not sure of the syntax of querying by dynamic ID.
I want to know how can i query by dynamic variable.
Thanks in advance.
$("#"+curID).find('input').attr("checked", true);
Or
$(this).parent().find('input').attr("checked", true);
Or
$('input', $(this).parent()).find('input').attr("checked", true); // using the scope argument
The selectors are strings... So should be handled as strings by concatenating the variables: and texts
$.click(function(){
var curID = $(this).parent()[0].id;
$("#"+curID+" input").attr("checked",true);
});
Your search is probably too specific. Break tasks down into their components instead of looking for a complete solution to a very specific problem. You are just dealing with basic string concatenation here.
You want:
var selector = "#foo input";
You have foo in a variable.
var selector = "#" + foo_variable + " input";

Categories

Resources