Getting started w/ jQuery and Javascript

Status
Not open for further replies.

Deffexor

Ars Tribunus Angusticlavius
7,607
Moderator
Howdy folks,
I'm currently knee-deep in writing a web app using ASP.NET MVC2 and I'm beginning to realize that this app could use some Javascript fanciness to make it operate better. I keep hearing from everyone to use jQuery to accomplish this, but am wondering where to start as I'm in that tough situation of "not knowing what I don't know" (well, regarding Javascript / jQuery.)

Currently, all I'm trying to accomplish is two (what I assume are simple) tricks:

1) When a radio button is clicked, two extra drop-downs appear on the form. When a different radio button is clicked, this disappear from the form.

2) Similarly, when a checkbox is clicked, two extra text fields appear. Unchecked, they disappear.

I suspect, I'll want to do more Javascript / jQuery as I get more involved in web programming.

What books should I read to get up to speed quickly on Javascript and jQuery?
 

technophile

Ars Legatus Legionis
23,768
I'd pretty much start here for jQuery; it's pretty straightforward, although if you know literally nothing about javascript you might not understand it. :) You will want to have at least a working knowledge of CSS (since jQuery uses CSS selectors) as well.

Can't recommend any good books, as the last time I consulted dead trees for anything javascript-related was circa 1998. :p
 
I'd just check out the jQuery tutorials and documentation.

As for your particular problem, it's fairly simple and mostly self documenting.

HTML:
Code:
<input type="radio" name="radiogroup_name" value="a" />
<input type="radio" name="radiogroup_name" value="b" />
<input type="radio" name="radiogroup_name" value="c" />

<div id="radioinputgroup" style="display: none;">
    <input type="text" name="input1" />
    <input type="text" name="input2" />
</div>

<input type="check" id="checkbox" />
<input type="text" name="input3" class="checkinputclass" />
<input type="text" name="input4" class="checkinputclass" />

JS:
Code:
$(document).ready(function() {
// Run the following when the page finishes loading
	// Bind a function to the change event for the radio buttons
	$('input[name=radio]').change(function(){
		if ($('input[name=radiogroup_name]:checked').val() == 'c'
			// show fields if the value of the checked radio button is C
			// the # indicates to select an element with ID 'radioinputgroup'
			$('#radioinputgroup').show();
		else
			$('#radioinputgroup').hide();
	});
	
	$('#checkbox').click(function{
	// Bind a function to the click event of element with ID 'checkbox'
		// toggle can also be used to show/hide fields
		// in this instance, toggle elements of class 'checkinputclass'
		$('.checkinputclass').toggle();
	});
});
 

technophile

Ars Legatus Legionis
23,768
Note that using "toggle" as dsaewra does is a bad idea if anything else on the page can change the state of the checkbox, since it uses a simple counter rather than a state check; if the toggle counter and the checkbox state get out of sync, the behavior will no longer be correct.

(It's also possible that using click() like that won't catch a keyboard-initiated check of the box, i.e. tab and then use space to mark it checked, but I've not tested that. If that's so, you would want to use change() instead of click().)
 

Deffexor

Ars Tribunus Angusticlavius
7,607
Moderator
skain: thanks for the rec. I've just ordered it up. I also ordered "Javascript: The Good Parts". Hopefully these 2 will get me up to speed.

dsaewra: I'm having trouble with this code. I'm developing in VS2008 using C# and ASP.NET MVC2 -- When I try to run this, I get an unhelpful error from the VS debugger that says, "Microsoft JScript runtime error: Object expected" -- And the debugger has the entire SCRIPT section of my HTML highlighted.

My JS and HTML (ASPX) looks like this:

Code:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IList<Sample>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"></asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<script type="text/javascript" language="javascript">
$(document).ready(function() {
// Run the following when the page finishes loading
   // Bind a function to the change event for the radio buttons
   $('input[name=radio]').change(function(){
      if ($('input[name=OrderType]:checked').val() == 'registerOrderVialsTubes')
         // show fields if the value of the checked radio button is registerOrderVialsTubes
         // the # indicates to select an element with ID 'vial-library'
          $('#vial-library').show();
      else
          $('#vial-library').hide();
   });
});
</script>
        
    <h2>Some Header</h2>

            <div id="vial-radio-boxes">
               <%= Html.RadioButton("OrderType", "registerVials", true)%> Register Vials only<br />
               <%= Html.RadioButton("OrderType", "registerOrderVials", false)%> Register Vials / Order Vials<br />
               <%= Html.RadioButton("OrderType", "registerOrderVialsTubes", false)%> Register Vials / Order Vials &amp; Tubes<br />
            </div>
            
            <div id="vial-library">
               <u>Library</u><br />
               <%= Html.DropDownList("Library")%><br />
               <u>Tube Format</u><br />
               <%= Html.DropDownList("TubeFormat")%>
            </div>

Any ideas?
 

Deffexor

Ars Tribunus Angusticlavius
7,607
Moderator
Hmmm... Lemme check.

EDIT: Nope, wasn't added to the Site Master page. Thanks for the tip.

Unfortunately, one of my dependencies (a web service) is offline, so I cannot run my app. Guess it's time for me to figure out this Dependency Injection (DI) stuff so I can continue testing... :)

I'll post back with my results when I get something working...
 
Status
Not open for further replies.