Fetch specific input element by attribure from Array - javascript

I have List of Check-boxes array and i want to get specific checkbox by specific attribute.
lets i have 20 checkedboxes in an array object. and each checkbox contains attribute tradeId
<input name="chkTrades" type="checkbox" tradeId="1">
i want to get only checkbox having tradeId=1
how to get it

You can select attributes in jquery pretty easily:
$('input[type=checkbox][tradeId=1]');
console.log($('input[type=checkbox][tradeId=1]')[0]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="chkTrades" type="checkbox" tradeId="1">
<input name="chkTrades" type="checkbox" tradeId="2">
<input name="chkTrades" type="checkbox" tradeId="3">

select check box by attribute
$('input:checkbox[tradeId=1]');

Related

jQuery Ajax checkbox value

I have checkboxes on my page for which I would like to send their state back to the database via ajax. I know how to use jquery with ajax and how to work with SELECT and OPTIONS, but I don't know to do the same things with several checkboxes and how to get the value from them.
Everything works when id is attached to one checkbox, but when i attach id to several checkboxes it doen'twork(except first check box)
Any ideas?
my code looks like this->
<form>
<div class="checkbox" >
<label><input type="checkbox" value="1" id="item">что то</label>
<label><input type="checkbox" value="1" id="item">Шорты</label>
<label><input type="checkbox" value="3" id="item">Классическая</label>
</div>
</form>
<script>
$(document).ready(function () {
$('#item').on('click',function(){
var name = $('#item').val();
$.post('load.php', {name:name}, function(data){
$('#name-data').html(data);
});
});
});
</script>
Use a class. Ids are expected to be unique on a single page. Classes are used to group elements together.
<tag id="a">A</tag><tag id="a">B</tag> $('#a')
becomes
<tag class="a">A</tag><tag class="a">B</tag> $('.a')
Id should be unique you can use class instead of Id.

Is there a way to fill multiple form fields at once?

Lets say I have a form with multiple fields like this :
<form>
<input type="checkbox" name="title45" value="title45"><input type="text" name="title45" /><br>
<input type="checkbox" name="title45" value="title45"><input type="text" name="title23" /><br>
<input type="checkbox" name="title45" value="title45"><input type="text" name="title36" />
</form>
Like this, there can be up-to 100 text fields. I need a way to select several or all of them and set the value once and the selected elements will get the value. How can I do this?
I will add a checkbox infront of the fields. I want to edit only the checked ones.
just this line of jquery :
$('form input').val("fgg");
see this in action: http://jsfiddle.net/c53nqn7e/
You get/set input values with .val(). So:
$('input').val('something')
You can narrow down the inputs selected in a variety of ways, like:
$('input[type="text"]').val('something') // select all text inputs
$('input[name^="title"]').val('something') // select all inputs with a name attribute that starts with title
you can do it like this:
$( ":input" ).val("your input value");
or
$( "form input" ).val("your input value");//better because it will apply only on form
but i'll suggest add class to these inputs and based on class assign the values so you can have some discrimination.

How to select a input with name and value

I need to select a radio input with name and value in jquery
In this example how you select element with name SiblingSex and value female
<form action="">
<input type="radio" name="SiblingSex" value="male">Male<br>
<input type="radio" name="SiblingSex" value="female">Female
<input type="radio" name="ParentSex" value="male">Male<br>
<input type="radio" name="ParentSex" value="female">Female
</form>
i need some thing like
$('input[name="SiblingSex"]' /*GENDER SELECTOR */)
You can add another attribute selector with this:
$('input[name="SiblingSex"][value="female"]').val();
the above line would give you values every time whether it is checked or not.
so if you only want to have the value when it is checked too then add :checked
$('input[name="SiblingSex"][value="female"]:checked').val();
Just have a look on the Demo on my JS Fiddle Code
Shows the two scenario:
1) when you want the value without selecting radio button.
2) when you want value after selecting radio button.
or may be the thing that you want is here::
JS Fiddle Demo

How to get the value of selected radio button in javascript/jquery

Here is the html part:
<input class="demo" radio-value="yes" name="Radios" id="sample1" value="" type="radio">
<input class="demo" radio-value="yes" name="Radios" id="sample2" value="" type="radio">
<input class="demo" radio-value="yes" name="Radios" id="sample3" value="" type="radio">
<input class="demo" radio-value="yes" name="Radios" id="sample4" value="" type="radio">
How can I know which radio button the user is selected and how can I know the value of selected radio button.
I want to get the "radio-value" of the selected button. Because, I will be using value for json input..
How can I do that??
First of all, all of your radio buttons have the same id - this is illegal and can cause unexpected behavior. Provided this is fixed, you can do something like this:
var selectedValue = $("input.demo:checked").val();
and for radio-value:
var selectedRadioValue = $("input.demo:checked").attr("radio-value");
Use :selected with class selector to get the selected radio button. You should give unique ids to html elements. Also the value of all the radio button is empty string you may need to assign some values to them
$('.demo:selected').val();
$('.demo:selected').val(); // class is demo
This would do the trick!
But you need to remove the id that is similar among all the elements. You should change their value such as sample1, sample2 etc. Then you can get the value, and the id.

get the value of input field of radio type in jquery

I have an input field like below
<input class="d_o" type="radio" value="super" name="old_or_new" checked="checked"> Get this value actually</input><br/>
I have to get the value of the input field i.e., Get this value actually, so tried the below jquery code
console.log($('.d_o').text());
But i am surprised that its returning nothing, and its working when tried to get the value like $('.d_o').val()
So how to get the text value from the above input field using jquery am i missing anything ?
The entirety of your <input> element is
<input class="d_o" type="radio" value="super" name="old_or_new" checked="checked">
The text that's after it, and the invalid html </input> are completely different nodes in the DOM tree. So val returns the value "super" as expected, but there's no text for text to return.
The .text() method cannot be used on form inputs or scripts
http://api.jquery.com/text/
Although I am not sure what approach jQuery follows on this : One explanation can be the "Content Model" specification of each HTMLElement .
Content model
A normative description of what content must be included as children and descendants of the element.
For example :
For Input type : http://www.w3.org/TR/html5/forms.html#the-input-element
Content model:Empty.
However for Title Element this is defined as
Content model : Text
http://www.w3.org/TR/html5/document-metadata.html#the-title-element
Eager to see the validations on this postulate :)
<input class="d_o" type="radio" value="super" name="old_or_new" checked="checked"> Get this value actually</input><br/>
This is not the correct way to handle the <input> tag. You should use :checked and .val() instead:
$(document).ready(function(){
$('#hey').on('click', function(event){
alert($('.check:checked').val());
});
});
<h2>Select old or new</h2><br>
<b>Old</b>:<br>
<input type="radio" class="check" name="old_or_new" value="old"><br>
<br>
<b>New</b>:<br>
<input type="radio" class="check" name="old_or_new" value="new"><br>
<br>
<input type="button" id="hey">
Check the fiddle: http://jsfiddle.net/MLWCK/

Categories