tags:

views:

72

answers:

3

i have one text field and setting its name & values from php, like:

<input type="text" id="qtyProductID_' . $productID . '" 
  size="3" value="' . $totalItems . '" >

i also have image button for click event

<a href=fun.php?action=update&productID=' . $productID . ' 
  onClick="return false;"><img 
  src="images/update.jpg" id="updateProductID_' . $productID . '"></a>

now after click in JS file i want to get value of text field i click

var productIDValSplitter = (this.id).split("_");
var productIDVal = productIDValSplitter[1]; 
var tqty = $('#qtyProductID_'.productIDVal[1]).attr('value');   
alert(tqty); 

i am getting error

i am looking for how to set the variable name in

$('#qtyProductID_'.productIDVal[1])

Thanks

A: 

.val() is what you're looking for

$("#someInput").val() 

What's the error?

brad
No i am not looking for that, i am looking for how to set the variable in line $('#qtyProductID_'.productIDVal[1])
air
ah, sorry, it's +, not .
brad
+5  A: 

In javascript the plus (+) sign is used for string concatenation, so

$('#qtyProductID_'+productIDVal[1]).attr('value'); 

but just like brad said, it can be written as

$('#qtyProductID_'+productIDVal[1]).val(); 

because jQuery offers a unified method of getting values from form input fields, not just <input>, but <select> and <textarea> as well.

macbirdie
A: 

your function can be completely rewrited as:

var target_id = parseInt(this.id.split('_')[1]);
var tqty = $( '#qtyProductID_' + target_id ).val();   
alert(tqty); 

then you say: i am looking for how to set the variable name in

$( '#qtyProductID_' + target_id ).attr('name' , WHAT_YOU_WANT_HERE );
aSeptik