user123456789
user123456789

Reputation: 2004

How to replace the first 3 characters in a textbox?

I have a textbox for the Origin of a flight. Any thing that gets entered into this textbox must be put at the front of the Routing textbox like in the image below:

enter image description here

The code to do this is:

function OnGetResult(result, sender){ 
    if(result.ID != -1){ 
        var text = document.getElementById('<%= txtRouting.ClientID %>');
        text.value = result.Code + ", " + text.value;
    }
}

So it simply adds the new code entered to the start of the Routing textbox. But my issue is if another code is entered into Origin then it just continues to add it to the Routing textbox. See in the image I entered in ATL to Origin but BOS stays in the Routing textbox.

enter image description here

How would I replace the first 3 characters in the Routing Textbox with the new value I put into Origin? But is there also a way to check that the first 3 characters belong to Origin? In the image the last code is coming from the To textbox. I only need to overwrite the origin so for example if Routing only had HAM then I enter in an Origin, I don't want to remove HAM.

Upvotes: 0

Views: 225

Answers (2)

Nandhini
Nandhini

Reputation: 354

Try something like below,

function OnGetResult(result, sender){ 
    if(result.ID != -1){ 
        var origin= document.getElementById('<%= txtOrigin.ClientID %>');
        var to= document.getElementById('<%= txtTo.ClientID %>');
        document.getElementById('<%= txtRouting.ClientID %>').value= origin.value+ ", " + to.value;
    }
}

Upvotes: 1

Fernando de Bem
Fernando de Bem

Reputation: 116

You can get the value for the routing TextBox, use split to separate before and after the coma and then change the value to the Origin value + "," + the second part of the split.

Or you can concat the routing value whenever there is a change to the Origin and To TextBoxes. Does this solve?

Upvotes: 0

Related Questions