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
Related
This question already has answers here:
Select inputs that doesn't have value attribute with querySelector
(2 answers)
querySelectorAll detecting value in input
(2 answers)
Closed 3 years ago.
I'm using the querySelectorAll() JS function to retrieve all elements under a datagrid table (aspx page) , that datagrid contains several checkboxes / textboxes , there's a save button which should only be enabled when the following rule is satisfied :
At least 1 checkbox needs to be checked OR at least one textbox needs to contain text ( not empty )
Is there a way to check if any of the textbox input elements returned by querySelectorAll() , has a value ?
function validate() {
var search = document.getElementById("myGrdID").children;
//var hasAnyText = search[0].querySelectorAll("input:valid");
var hasAnyChecked = search[0].querySelectorAll("input:checked");
console.log("Any check? " + hasAnyChecked.length);
}
i'd like to avoid using Jquery , already managed to get all the checked input elements , couldn't find a way yet to find all the textboxes with values (any value not a specific string).
Are you able to use the filter function ?
var results = hasAnyChecked.filter(input => input.value != null)
Edit : comment below me has a point, but if you use those kind of selectors you won't be able to check multiple scenarios :
var results = hasAnyChecked.filter(input => input.value !== null || input.value !== false) // Handles both text inputs and checkboxes
This question already has answers here:
jQuery attribute selector variable
(3 answers)
Closed 5 years ago.
I am having some elements which contain name=kra[0][category] , name=kra[1][category].. and follows.
But when i select this elements by name i am unable to do it.
my jquery code:
$("[name = kra[0][category]]").prop("disabled" , true);
If I understand you and kra is a javascript array you can try:
$('[name = "' + kra[0][category] + '"]').prop("disabled" , true);
Your array was IN the string, it has to be outside :)
The following works (assuming it concerns an input element):
$("input[name='[kra[0][category]]']").prop("disabled" , true);
The name must be between quotes when it is not a single word.
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);
This question already has answers here:
Get attribute name value of <input>
(11 answers)
Closed 9 years ago.
I have this submit button.
<input id="bulkBtn" name="cat" value="delete" />
Now I want to get access to name attribute of this Jquery Object.
var bulkBtn = $('#bulkBtn');
//how can I get to bulkBtn's name attribute???
Does anyone know how to do it?
I know I can get access to name attribute of input tag like this.
But, I want to know how to get to name attribute that is contained in Jquery Object.
$("input[name='foo']");
Thanks in advance!!!
var name = $("#bulkBtn").attr("name");
Using the attr function:
var bulkBtn = $('#bulkBtn');
var name = bulkBtn.attr('name');
You can use var name = $('input').attr('name'); you can get any attributes you want like $('input').attr('id'); for single element.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Use javascript variable in object name
I am using CKeditor as a rich text editor. I have dynamically generated textareas with a unique ID that need replacing with text editors. That is working fine, but I then need to call getData(); on the textarea to get the data for an AJAX call. This is easy enough:
var editor_data = CKEDITOR.instances.editor1.getData();
The problem is I need editor1 to be dynamic, depending on the value of an attribute on a button. I record the textarea's identifier in a sibling button's name attribute:
var INSTANCE_NAME = $(this).attr('name');
Logging that out I get the correct editor ID back. (Note only using CAPS to highlight where it needs to be used in the next code block.)
I now need to use that INSTANCE_NAME as a variable like so:
var editor_data = CKEDITOR.instances.INSTANCE_NAME.getData();
I imagine my entire code needs to look something like this:
var INSTANCE_NAME = $(this).attr('name');
var editor_data = CKEDITOR.instances.INSTANCE_NAME.getData();
But I just get an error that CKEDITOR.instances.INSTANCE_NAME is undefined (which isn't surprising really)
Thanks
There are two ways to access properties in an object:
object.property
object['property']
Because the second option takes the property name as a string, you can build it dynamically — in this case, using the string INSTANCE_NAME:
var INSTANCE_NAME = $(this).attr('name');
var editor_data = CKEDITOR.instances[INSTANCE_NAME].getData();
// \_____________/
// ^
Use square brackets:
var editor_data = CKEDITOR.instances[INSTANCE_NAME].getData();