Pam
Pam

Reputation: 77

Need to get value of a text field, using a concatenated string for the id

Hi I'm not very good at explaining this in the right terms but I'm trying to retrieve the value of a textbox. The textboxes are in a table and each row has one and the id of each is made up of a unique id and a name... html

<label id="labelref<?php echo $row_list['pp_order_details_id']; ?>" name="labelref<?php echo $row_list['pp_order_details_id']; ?>"></label>
<input type="text" id="save<?php echo $row_list['pp_order_details_id']; ?>" name="saveref<?php echo $row_list['pp_order_details_id']; ?>" style="margin-right:5px;" />
<a href="javascript:saveref('<?php echo $row_list['pp_order_details_id']; ?>');" id="aref<?php echo $row_list['pp_order_details_id']; ?>" style="margin-right:5px;">Save</a>
<label id="errorref<?php echo $row_list['pp_order_details_id']; ?>" style="color:red; margin-right:5px;"></label>

javascript/jquery

function save(order_id) {
    var newSaveRef = "saveref"+order_id;    
    var saveref = $('#newSaveRef').val();
    alert(saveref); 
}

I want the value of save ref. If this value ($row_list['pp_order_details_id'];) is SS16363 then this value (newSaveRef) should be saverefSS16363.

but how do I get the value from the textbox with that ID????

Upvotes: 1

Views: 388

Answers (5)

TRR
TRR

Reputation: 1643

$("#newSaveRef").val();

Has to be:

$('#' + newSaveRef).val();

Upvotes: 1

stay_hungry
stay_hungry

Reputation: 1448

try this one

var saveref  =  $('#saveref' + order_id + '' ).val();

Upvotes: 0

Rab
Rab

Reputation: 35572

function save(order_id) {
    var newSaveRef = "saveref"+order_id;    
    var saveref = $('#'+newSaveRef).val();
    alert(saveref); 
}

Upvotes: 1

Jeff Watkins
Jeff Watkins

Reputation: 6359

function save(order_id) {
    var newSaveRef = "#saveref"+order_id;    
    var saveref = $(newSaveRef).val();
    alert(saveref); 
}

You were using a string literal rather than the variable itself.

Upvotes: 2

Jamiec
Jamiec

Reputation: 136124

newSaveRef is a variable, so concatenate it with a hash # to use the variable to find an element with that id.

var saveref = $('#' + newSaveRef).val();

Upvotes: 3

Related Questions