Y2theZ
Y2theZ

Reputation: 10412

Jquery/Javascript get id of a textbox using the id of another textbox

I want to use a jQuery/JavaScript function to validate the input of a textbox.

I have pairs of checkboxes like this:

<input type="text" id="MinMondayTxt" />
<input type="text" id="MondayTxt" />

this is for each day of the week (MinTuesdatTxt, MinWednesdayTxt...)

The Function is like this:

function ValidateTxt(TxtId){
}

It take the Id of a textbox as input. What I want to do is the following:

1) Check if the Id starts with "Min"

2) Get the corresponding textbox, by removing "Min" from the id. for example if we have MinMondayTxt I want to remove the "Min" in order to access the MondayTxt textbox

3) Compare the the values of the 2 textboxes (all values are numeric)

4) if MinMondayTxt > MondayTxt Alert the user

I am looking for help for step 1) and 2) : How can I check if the id starts with "Min" and how to get the corresponding textbox id (by removing the min)

Upvotes: 1

Views: 441

Answers (3)

jbduzan
jbduzan

Reputation: 1126

maybe you could try with split:

txtId.split('Min');

if Min exist it should create an array like that :

array = ["Min","MondayTxt"];

then you just have to use array[1].

Upvotes: 1

Pranay Rana
Pranay Rana

Reputation: 176896

You can use replace() method with the #id selector of jquery as below

function ValidateTxt(TxtId){ 
 var withoutminid = TxtId.replace(/^Min/, '');

 var withouminidval = $('#' + withoutminid).val();
 var minidval = $('#' + TxtId).val();
} 

Upvotes: 2

Amadan
Amadan

Reputation: 198324

This will remove "Min" if present, then fetch the jQuery object:

$('#' + TxtId.replace(/^Min/, ''))

Upvotes: 1

Related Questions