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.
Related
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:
How an I get all form elements (input, textarea & select) with jQuery?
(13 answers)
Closed 6 years ago.
In jQuery, what is the equivalent to document.getElementById(“formid”).elements["foo"].value ?
I am trying to get the value of the elements in a form
Thanks.
This gets the value of the input element with name foo in the form with id formid
$('form[id=formid] :input[name=foo]').val();
In jQuery you can get any element like this:
var form = $("#formId");
This does the same as document.getElementById(“formid”).
Furthermore, you can iterate through your form and get all of it's values like:
var inputValues = [];
$('#formId input, #formId select').each(
function(index){
var input = $(this);
inputValues[input.attr('name')] = input.val();
}
);
This question already has answers here:
Detect element tag name by its class
(5 answers)
Closed 9 years ago.
Hi guys i only wanted to ask how to get the tag name of an element that has a class name, im trying this:
function copyElement(){
$('body').keypress(function(event) {
if(event.which == 99){
var elementToCopy = $('.highlight').attr('tag');
alert(elementToCopy);
var newElement = $(document.createElement(elementToCopy));
}
});}
But im always getting undefined! :( help please.
Try this
$('.highlight').prop("tagName").toLowerCase();
var tag = this.nodeName.toLowerCase(); // native JS
var tag = $(this)[0].nodeName.toLowerCase(); // the jquery equiv.
var tag = $('.SomeClass')[0].nodeName.toLowerCase(); // the jquery equiv. with selector
This will go through each tag with that class.
$('.highlight').each(function() {
alert(this.tagName);
});
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:
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();