Getting Form Values with Javascript

I am building a CRM/Scripting System/Scheduler for a client and I needed an easy wrapper function to quickly grab the value of a form input with javascript. I use alot of AJAX and was noticing that I was repeating the same set of code over and over and over and over…. You get the jist πŸ™‚ In programming this is a bad idea. You want to strive for reusable code. Now the code I was writting over again is pretty simple but I still wanted to save myself some typing, another thing you strive for as a programmer, so I came up with a couple simple wrapper functions that will get me the value of a form input with a single function call.

Without further ado:

/**
 * Returns the value of a select box. It will 
 * work for both a multiselect box and a normal
 * select box.
 *
 * The function returns an string of the selected
 * values separated by a ','.
 */
function getSelectValue(selID) {

	var selectList = document.getElementById(selID);
	var optionsLength = selectList.options.length;
	
	var results = "";
	
	for (gsv_i = 0; gsv_i < optionsLength; gsv_i++) {
		if (selectList.options&#91;gsv_i&#93;.selected) {
			value = selectList.options&#91;gsv_i&#93;.value
			if (results == "") {
				results = results + value;
			} else {
				results = results + "," + value;
			} 
		}
	}

	return results;
}

/**
 * Returns the value of a form input.
 */
function getInputValue(id) {

	if (document.getElementById(id).type.indexOf('select') != -1) {
		// We have a select element. Lets call the function for selects.
		return getSelectValue(id);
	} else {
		// Else we will just return the value of the element
		return document.getElementById(id).value;
	}
	
}

/**
  * USAGE 
  */

function getTestValues() {
	var inputValue = getInputValue('textInput');
	var selectValue = getInputValue('selectInput');

	alert(inputValue);
	alert(selectValue);

	return false;
}

&#91;/sourcecode&#93;

And now for the HTML:

&#91;sourcecode language='html'&#93;
<input type="text" name="textInput" id="textInput" value="Some Value" />
<select name="selectInput" id="selectInput">
	<option value="option 1">Option 1</option>
	<option value="option 2">Option 2</option>
	<option value="option 3">Option 3</option>
</select>

<script type="text/javascript">
	getTestValues();
</script>

Since I’m not a Javascript guru I am not guaranteeing this is the best way to do this. It is just what I found most convenient for me and I believe will be convenient for others πŸ™‚

I hope you enjoy and if you have questions feel free to ask πŸ™‚

Later!
– Miah


I believe that’s the dance of a brave little toaster.” – Xander, Something Blue

Advertisements

Week 1 Update

Well the first week is over. It is starting to be a slow start but I have good feelings that things will soon slide into place.

On the food front, it was rough. Wednesday my fiancΓ©e got sick and we stayed home. I worked from here but our schedule was thrown off so we didn’t quite eat as much or what we should have. The next few days did get better. Then today we slipped again. I stayed up late last night and woke up late which again threw off the schedule. Things will get back into place πŸ™‚

Friday with the trainer was good. He had us focus on legs and biceps. Surprisingly I could walk this weekend! πŸ™‚ I attribute it to the protein shakes we are taking with the nutrition plan.

Well this is a short update. It’s late and I’ve been working all day and still have a few things to get done before I can call it a night.

– Miah

Updating my Fitness Resolution

OK, I have spoken with my personal trainer (yes I have a personal trainer πŸ™‚ ), and we have adjusted what this goal will be. He doesn’t want me to focus on loosing 100 pounds off the bat. He has bigger and better plans for me πŸ™‚ Also he is right in how people tend to get discouraged if they set such a lofty number. This is why I am changing the weight loss resolution to this:

To stick with a fitness plan and increase my overall health throughout the year.

I may not be loosing 100 pounds but in total more like 50-70 pounds. This is because I am not only doing cardio and weight loss exercising but I will be doing weight training as well to build more muscle. This will allow me to loose the body fat, gain muscle and be in an overall healthier state and since muscle is heavier then body fat this is why it will be more difficult to loose 100 pounds. I’m cool with the plan and am excited to get some results.

The basic plan is this. I am on a nutrition plan that will give me all the nutrients I need to keep up with the plan. I’m taking a series of natural vitamins and making my own protein shakes. I will be eating every 2 1/2 to 3 hours and will be drinking at least 8 glasses (8 oz each) of water a day. I have purchased a food diary so I can mark down what I am eating and the exercise I am doing. This will allow me to make sure I stick with a good diet and not stray and eat fast food or other unhealthy foods as my trainer will be looking each session what i have been eating πŸ™‚

As for the exercise I meet with my trainer twice a week, every Monday and Wednesday morning. At training I will be doing 30-45 min of cardio on the treadmill or elliptical machine then 45 min – 1 hour worth of weight training to tone up and build muscle. In addition to this I will be doing cardio 3 more times a week, Friday, Saturday and Sunday mornings. Either on a treadmill/Elliptical machine or outside on one of the many trails is Sarasota. The other two days, Tuesday and Thursday will be days to rest up and cook meals ahead of time for the next day.

Since I am here I will give a quick update on how this week has gone so far. I will normally give an update each weekend on Saturday or Sunday for the whole week.

Yesterday was the first day back with the trainer after the holidays. I was glad to be back and starting to work on my fitness. Did 25 minutes on the treadmill and 45 minutes of weight training. I’m a little sore today in the shoulders but not too bad. I had expected my legs to feel it more but surprisingly they are fine. As for the meal plan. I took my morning Vitamins but missed out on the afternoon set. I had a good breakfast of Oatmeal and Fruit and my lunch and dinner consisted of 1/2 of a Publix sub each meal. I had some Fiber Selects wheat thins for snacks (garden veggie flavor) and fell victim to two slices of pizza at the end of the night (grrrr) . Not too happy with that but I got home late and needed to eat something. The pizza was left over from the weekend and I would feel bad wasting it. If anyone wants to take it off my hands feel free πŸ™‚ lol.

Come this weekend I’ll give an update of the week. I’m feeling good about this! w00T!

– Miah


To eat is a necessity, but to eat intelligently is an art.La Rochefoucauld