2

i want to get the value of textbox on the basis of its class and id. this what i have done but it is not working for me. although i can set the value of textbox by doing this.Please help me out

<script>
....
var count=1;
var check=$(".AssVal,#"+count).val();
....
<script>
<input type="text" value="0" class="AssVal" id="1"  maxlength="20" size="20" />

This is how i set the value in textbox

var check=$(".AssVal,#"+count).val(123);
Subodh Bisht
  • 859
  • 7
  • 19
  • 33

4 Answers4

2

Since an ID attribute is unique, you can just use the ID on it's own:

// Get value
var check=$("#"+count).val();
// Set value
$("#"+count).val(123);

But the problem with your selector is that jQuery is trying to find all .AssVal elements and all elements with id=1 as a comma in a selector denotes attempting to join multiple selectors.

CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
0

use class selector . is used for selecting classes and # for ID

var testvalue = $(".AssVal").val()

or

var testvalue = $("#1").val() // try not using no's as id
0

Using jQuery, you can get the value simply by not passing any arguments to the val() function you've already used. This would look like: var myValue = $(".AssVal,#"+count).val();

alexpls
  • 1,914
  • 20
  • 29
0

You can set the value using class as below

 $(".AssVal").val("123");

By ID as

$("#1").val("123");

For getting

alert($("#1").val());
alert($(".AssVal").val());

And for dynamic

alert($("#"+count).val());
Amit
  • 15,217
  • 8
  • 46
  • 68