Picking value from Text Box in jquery by ID - javascript

I am having 3 textbox where the ids are
"ctl00_m_g_8b41d68e_7c2d_49c3_a4b5_316440ad77e1_SpListPicker_SelectionBox", "ctl00_m_g_9b41d68e_7c2d_49c3_a4b5_316440ad77e1_SpListPicker_SelectionBox" and "ctl00_m_g_8a41d68e_7c2d_49c3_a4b5_316440ad77e1_SpListPicker_SelectionBox".
Now the id which is getting generated dynamically, but the "SpListPicker_SelectionBox" is constant.
Now I am having a requirement, onClick of a button either of the 3 textbox will have the value, which is to be fetched. How can I get the value of textbox.
Any help is appreciated. Can we get the value on the like operator? where ID like '%SpListPicker_SelectionBox' ??

Try this:
var $inputs = $('input[id$="SpListPicker_SelectionBox"]')
Where $= is the Attribute Ends With Selector
Then you can loop through each textbox and get the values like:
$inputs.each(function( index ) {
alert( index + ": " + $(this).val() );
});

use attribute selector with $ ,this fetch all the elements ending with the given string..
docs
$('input[id$="SpListPicker_SelectionBox"]').val()

you can write Javascript or jquery code runtime by using code while textboxs generated and use exact textbox id associate with jquery statement.

Related

How to set the value of a form input field without ID using Javascript?

How to set the value of a form input field without ID using Javascript?
I can set the ID for the form, but i can't set an id to it's input.
Use id for your input like this:
document.getElementById("myID").value = "Hello"
You must use ID's for your input, because I don't know how many forms you have.
Here you can see the example:
Input Text Value
If you are looking for a specific tag like all your 'form' or 'input' tag, you can do something like this:
function myFunction() {
var list = document.getElementsByTagName("UL")[0];
list.getElementsByTagName("LI")[0].innerHTML = "Milk";
}
Here is the example link:
getElementsByTagName example
For more info:
getElementsByTagName
You can use query selector to find the input and set its value like below:
document.querySelectorAll("input")[1].value = 'MEEEW';

Get value of a specific input using ".find()"

I want to get the value of an input. In my application, I want to update an entry in my database for each row. So, when I click on the button, I want the values of the corresponding row.
$('.ajouter_un_sol').click(function(){
var id_sol = $(this).find('.nom_sol').value;
alert(id_sol);
});
I made a jsfiddle for you to see what I'm trying to do. I'm trying to get the value of a row.
Can someone tell me how I should do please ?
use this
$('.button').click(function(){
var age = $(this).parent().parent().children('td').find('.age').val();
alert(age);
})
use .val() in jquery.
The .val() method is primarily used to get the values of form elements
such as input, select and textarea.
var id_sol = $(this).find('.nom_sol').val();
for your scenario use
$(this).parents('tr').find('td .age').val();
Fiddle
Try
var id_sol = $(this).closest("tr").find('.nom_sol').val()
.value is a javscript property so you need to use .val().
You are trying to find the input inside button but actually it is within td so you need to find it in td within the parent tr
In your fiddle, instead of
var age = $(this).find('.age').value;
try
var age = $(this).closest('tr').find('.age').val();
closest('tr') finds the nearest ancestor of type 'tr', and then you search down for the age field from there.

how to get value in javascript from document.getElementsByClassName('myTxtBox');

The html for the textbox I was to get the value from is created from a .js file -- from javascript. I tried this to get the value that I enter into myTxtBox which is defined by a className:
<input type='text' class='myTxtBox editable' name='myTxtBox' value='' maxlength='200' size='90'/>
....
I try to retrieve the value I enter into myTxtBox as follows:
var txtval = document.getElementsByClassName('myTxtBox');
alert(txtval);
...more stuff where I set a breakpoint
the alert says I have [object htmlcollection]
intellisense does not give me .value -- I only get txtVal.valueOf, but when I break into the code and hover over txtval I get a listing for >Methods, ..., >myTxtBox. When I expand the >myTxtBox list if I scroll to the bottom of that listing I DO see "value" in >myTxtBox list and it DOES equal what I entered on the web page.
How do I retrieve that value? I have tried all sorts of options from the intellisense, but it either gives an error msg or [object htmlcollection] on the alert. How do I retrieve the value I entered? Or -- do I use something different than document.getElementsByClassName('myTxtBox') for my scenario?
You would need to return the index + value as getElementsByClassName returns a HtmlCollection so there are many elements to it.. try this:
var val = document.getElementsByClassName('myTxtBox')[0].value
alert(val)
getElementsByClassName returns a HtmlCollection which is array like. Do this like this:
var txtval = document.getElementsByClassName('myTxtBox')[0].value
alert(txtval)
I discovered that I could add an ID to my input element 'myTxtBox' and use jquery to retrieve the desired value, so I did this -- added an ID to the textbox and use jquery in the alert to do a document.getelementbyID
//--generate the html section here
"...<input type='text' class='myTxttBox editable datepicker' id='myTxtBox' name='myTxtBox' value='' size='10'/>..."
function NextButton_Click()
{
try
{
alert($("#PositionStartDateTextBox").val()); <---and this displays the value entered into this textbox
....
The whole deal is I have to evaluate some date textboxes because users can enter values in manually -- I guess the best fix would be for this textbox to not be editable. That would be another question -- how to add a datepicker and not have an editable textbox.

How to insert selected element from multiple selection into an input?

hi everyone i have a problem.
i have a multiple selection and i want to select something and put it into an input through a button i hope i have been clear :
i manage to get the select item with this jquery code :
var chosen= $('#droite option:selected').val();
droite is an id for the multiple selection
and i want to put it into the input wich has an id : chosen item here is my jquery code:
$("#chosenitem").prepend(chosen);
and it won't work do you have any idea why .?
You need to call val() on the select itself, not the options it contains:
var chosen = $('#droite').val();
Similarly, to set the value of the #chosenitem input, use val() with a parameter:
$("#chosenitem").val(chosen);
Note that if multiple options are selected in the #droite element, the value returned will be a comma delimited string, eg. foo,bar,baz.
You should call val() to set the value of #chosenitem
$("#chosenitem").val(chosen);

assigning initial value of input text field with jquery

I am trying to assign an initial value to a text box with using jquery. This is the text field
<input type="text" class="info"/>
This creates more than one text fields and I need to populate the text fields with initial values which come from database. I am having problem since I am trying to it with jquery adapter.
Any clue on this?
Thanks in advance.
Here is the detailed code:
I include index.html
<?php include "index.html"?>;
which puts some text fields to record.php:
...
...
After including index.html I am using this code to assign intial value to the text boxes which have a class name "info":
$('.info').each(function() {
$('.info').val('yourvalue');
});
Instead of assigning a constant value (here it is "yourvalue") I am going to assing some database records to each input fields.
$('.info').val('yourvalue');
replace yourvalue with the value you want to set..
Set this value in the success callback of your ajax request..
$('.info').val('value-to-be-set');
.val( value ) - Set the value of each element in the set of matched
elements
Above code will set the value of the input fields where the class name if 'info'
Read More here
$('.aciklama').each(function() { $('.aciklama').val('yourvalue'); });
you know $('.aciklama') is an array , and when you iterator you array get the object, you still get the $('.aciklama') array and set the array value, as I see It is not correct.
You should write like this:
$('.aciklama').each(function(index, item) { $(item).val('yourvalue'); });

Categories

Resources