Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing only part of an input field

Status
Not open for further replies.

weibs

Programmer
Dec 17, 2008
61
US
I have an auto-complete form at
The array looks somewhat like the following.
Code:
$("#City").autocompleteArray(
		[
        "ANCHORAGE (ANC)", "AKIACHAK (KKI)", "ANIAK (ANI)", "ATMAUTLUAK (ATT)", "ATQASUK (ATK)", "BARROW (BRW)", "BETHEL (BET)", "FAIRBANKS (FAI)", "HOMER (HOM)"
		],
I need it to display what is inbetween the quotes, but I only want it to pass what is inbetween the brackets and then put into a hidden field called departCity.

Hope that made sense.

Is there anyway to do this?

Thanks in advance.
 
Show us what you've done, tell us what doesn't work, and we'll help you. If you're looking for free programming to be done for you, you'll be disappointed.

Lee
 
Have you tried the split function, on your textbox value?
Code:
var myvalues=textbox.value.split("(");
Then just take the second value of the produced array, and remove the last character.
Code:
var myfinal=myvalues[1].substr(0,myvalues[1].length-1);
alert(myfinal);

This of course assuming there is no more text after the the closing ")" in your values.






----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thank you vacunita for pointing me in a direction. Unfortunately I still cannot get it to work.

If I add the split function into the existing code

Code:
function findValue(li) {
	if( li == null ) return alert("No match!");

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;


 var myvalues=sValue.value.split("(");
 var myfinal=myvalues[1].substr(0,myvalues[1].length-1);
 alert(myfinal);
	 // alert("The value you selected was: " + sValue);
}

It will not even throw up an alert.

Forgive me. I'm just coming from having a stroke with brain injuries and trying to pull my head thru this has been quite the task.

Thank you for your help.

 
How is it not working? is the last alert not showing the correct value? or Is it just not doing anything?

What exactly would be the contents of sValue?

I'm not sure sValue would have a value property or if it actually holds the value itself.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
With this code the alert will pop up with the total value of the selection ie: The value you selected was ANCHORAGE (ANC)

Code:
<input type="text" name="City" id="City" value=""  style="width: 200px;"/>
<script type="text/javascript">
function findValue(li) {
	if( li == null ) return alert("No match!");

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;


// var myvalues=textbox.value.split("(");
// var myfinal=myvalues[1].substr(0,myvalues[1].length-1);
// alert(myfinal);
	 alert("The value you selected was: " + sValue);
}

But with changing the variables at the bottom to
Code:
var myvalues=sValue.value.split("(");
var myfinal=myvalues[1].substr(0,myvalues[1].length-1);
alert(myfinal);

It will not even produce an alert.

:(

If you need to see the code in action, you can view it here.
 
Ahh see o.k. sValue holds the actual value, which means it doesn't have an extra value property to it.

So you want to just address the split function directly:

Code:
var myvalues=sValue[red].split("(")[/red];

The rest should remain the same.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 

Ahhhh success!!

Now I just need to figure out how to pass that value to a hidden field called departCity for the Departure City and returnCity for the Destination. Once I get those values into a hidden field the form can be submitted properly.

Thanks!!
 
If your hidden fields have a Id just use that with getElementById, and modify their value property.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Ok, nothing is working :(

I have tried adding
Code:
Search for a Departure City:<br>
<input type="text" name="City" id="City" value=""  style="width: 200px;" />

<br>
 <div>Search for a Destination City:<br>
  <input type="text" name="Destination" id="Destination" value="" style="width: 200px;" />
<br>     
<input type="hidden" name="departCity" id="departCity" />
<input type="hidden" name="returnCity" id="returnCity"/> 

<script type="text/javascript">
function findValue(li) {
	if( li == null ) return alert("No match!");

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;
var myvalues=sValue.split("(");
var departCity2=myvalues[1].substr(0,myvalues[1].length-1);
alert(departCity2);
	 // alert("The value you selected was: " + sValue);
var departCity = document.getElementById('departCity2');

	var userInput = document.getElementById('departCity2').value;
	document.getElementById('departCity').innerHTML = userInput;

}

// Stipped out rest of code
</script>

The part that was added was the following
Code:
	var userInput = document.getElementById('departCity2').value;
	document.getElementById('departCity').innerHTML = userInput;
But it doesn't pass the information into the hidden field called departCity.

I can't for the life of me figure out how to accomplish this.

Thanks again for your help.
 
You really need to read into Javascript a bit more.
You can't just make up properties for things as you go along.

1.
departCity2 is a variable, that already holds the value you want to pass, so this:

var userInput = document.getElementById('departCity2').value;

Makes no sense and even if it worked, its pointless.

The value is already readily accessible just by using departCity2. Nothing more.

2.
departCity is an input, it doesn't have an innerHTML property, but rather a Value property you can alter so:

All you would need to do is:

document.getElementById('departCity').value = departCity2;



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Oh thanks, that is awesome. And yes, I agree that I need to learn more on Javascript, just under a time crunch and my brain isn't functioning well.

The script is now passing the values, however, with having two fields, it's passing the last field twice for both fields. *shakes head*

Here is my code now.
Code:
var myvalues=sValue.split("(");
var departCity2=myvalues[1].substr(0,myvalues[1].length-1);
alert(departCity2);
	var departCity = document.getElementById('departCity2');
document.getElementById('departCity').value = departCity2;

var returnCity2=myvalues[1].substr(0,myvalues[1].length-1);
alert(returnCity2);
	
var returnCity = document.getElementById('returnCity2');
document.getElementById('returnCity').value = returnCity2;

Any other suggestions?
 
The script is now passing the values, however, with having two fields, it's passing the last field twice for both fields. *shakes head*

Of course it is, you are using the same source for both:
The sValue variable to be exact.

sValue is coming from a li.selectValue which I assume somehow gets its value form your first drop down textbox.

You would need a sValue2 that gets its value from your second drop down textbox, but I have no idea how that works.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
That's what I was afraid of... guess it's back to the drawing board. I have no idea how to separate the svalue from that script.

Thanks for all the help you have given.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top