If I've got a jQuery object that contains input fields like so:
var $inputs = $("#signup input");
Is there a way I can directly select one out of it where the name attribute equals a given value?
I've tried $inputs.find("name=firstName").select();, but it seems not to be working.
You need to use attribute-value selector here:
$("#signup input[name=firstName]");
update:
$inputs.filter("[name=firstname]");
Working Demo
You were close.
$inputs.filter("[name=firstName]").select();
Or, using my preferred syntax...
$inputs.filter('[name="firstName"]').select();
Demo
http://api.jquery.com/attribute-equals-selector/
Use:
$inputs.filter('[name="firstName"]');
Special Note:
.select() is used to select the text in the input but not to select the way you mean.
Try
var $inputs = $("#signup");
$("[name=firstName]", $inputs).select();
jsfiddle http://jsfiddle.net/guest271314/571ckuu0/
Related
i want to use a input box from SELECT2(jQuery Plugin) and want to send it with PHP Post to the server.
How do i access these Elements (A, Adolf, B)?
Thanks
$('.select2-search-choice div');
if you want to get the text of the elements, then use $('.select2-search-choice div').text();
If you use Select2 (http://ivaynberg.github.io/select2/) it overrides <select> element.
So probably you use like this:
<select id="my-select"><option>...</option></select>
And in JS:
$("#my-select").select2();
This way - if you want to read current value just use:
$("#my-select").val();
or using Select2 API:
$("#my-select").select2("val");
If you need to get all values from <option>'s - you can do it like this:
$("#my-select option").map(function() {return $(this).val();}).get();
You can loop through $('li.seleact2-search-choice').children('div'); or simply $('.select2-search-choice div');
To get an Array of the values use the API from select2:
var data = $("#YourSelect2Element").select2("val");
You have to change the selector in the selector you used to create the select2 input.
See the docs.
I have an input text field like below
<input type="checkbox" value="9961103777" name="mobile[]">
I just want to get the value using jquery. I tried this but not working:
jQuery('input[name=mobile[]]');
Please help me in this.
You can use val() as well as wrapping the name of your input inside double quotes " ":
jQuery('input[name="mobile[]"]').val()
Put attribute value in quotes. To fetch its value use .val()
jQuery('input[name="mobile[]"]')
DEMO
try
$("input[name='mobile[]']").val();
try this ..
Refer this post Jquery get input array field
$('input[name^="mobile"]').each(function() {
alert($(this).val());
});
JS Fiddle Example
I have been searching for the solution for this for a while so I hope I can get some help now. I am attempting to figure out whether a programmatically added checkbox is checked using jquery. The HTML code of the checkbox is below:
<td>
<input type="checkbox" name='ismanual' id='ismanual' class="checkbox">
</td>
And I am doing it using the following jquery code:
var ismanual = document.getElementById("ismanual").val();
I have attempted using #ismanual as the selector, but that didn't help. Any idea where I have gone wrong?
If you're using document.getElementById("ismanual") you should use .checked instead of .val()...
var ismanual = document.getElementById("ismanual").checked;
For jQuery use...
var ismanual = $("#ismanual").is(':checked')
Where you have gone wrong: if you want to use val(), you need to use it on a jQuery object. getElementById() gives you a DOM element but not a jQuery object. To get a jQuery object you need to find the element using a jQuery selector like this:
jQuery('#ismanual')
You can then use jQuery's :checked selector and the .is() function on that object:
var isManual = jQuery('#ismanual').is(':checked');
.val() is a jQuery function. It will not work with DOM style element selection.
To get value using .getElementById(), you must use .checked like,
var ismanual = document.getElementById("ismanual").checked;
.val() will work when you are selecting the element using jQuery like,
$("#ismanual").val();
I have a dynamic html table generated using javascript. the table contains diffrent controls like textbox,dropdown box which has custom attributes.How can I loop through all the controls present inside this table and find the control whose custom attribute matches to some value?
This will give you all the form elements inside your table (:input selector):
var $formElements = $('#tableid').find(':input');
You can filter with an attribute selector:
//will select every form element having a data-custom attribute set to 5
var $formElements = $('#tableid').find(':input[data-custom="5"]');
Please see the jsFiddle Demo. For my examples I used HTML5 data- attributes, but the code will work with any attribute you need.
OR you can use the filter() method to write a function that filters your elements:
var $formElements = $('#tableid').find(':input').filter(function () {
return $(this).attr('data-custom') == '5';
});
jsFiddle Demo with filter()
Demo : http://jsfiddle.net/DSqZr/1/
function getControl(_value){
$("#panel :input").each(function(){
if($(this).attr("custom") == _value){
return $(this);
}
})
}
var selectedCrl = getControl(1);
You can use Attribute Contains Selector.
Check the example it is probably very close to what you need which is finding input & select elements with certain attribute values
Give them a class .control and:
$('.control[attribute=value]')
Check Selectors API for more on attribute selectors.
I want to get the contents of a span tag but what I have isn't working. An alert shows an empty dialog box. I've tried the following but nothing works.
var test=$('#test').val()
var test=$('span#test').val()
var test=$('td span#test').val()
Can someone tell me what I'm doing wrong?
span elements do not have a value property.
Instead, use html() for the HTML or text() for the text nodes.
$('span#test').text()
will give you the text. val() is for the attribute value
You are correct almost, but instead of the val() to Need to say html().
var test=$('#test').html();
var test=$('span#test').html();
var test=$('td span#test').html();
a span doesn't have a value attribute.
You should instead do the following
var test=$('span#test').html()
or
var test=$('span#test').text()
use this
var test=$('#test').html()
You should use .text(). .val() is only for input elements.
var test = $('span#test').text();