Jump to content

attaching a change event


jimfog
 Share

Recommended Posts

I am trying to attach a change event(usuing jquery) to a form radio button such that when the user selects either of the radio buttonsan alert message appear-it is for testing only. Here is the html:

<form action="" method="post">  <input type="radio" class="formBuzType" name="buztype" value="5"> salon spa <br>  <input type="radio" class="formBuzType" name="buztype" value="4"> car <br>	    </form>

and here is the js code:

('.formBuzType').change(function(){alert('hi');});

On Google Dev Tools I get the following error message:Uncaught TypeError: Object .formBuzType has no method 'change' WHat the above means?

Link to comment
Share on other sites

Ι cannot believe I forgot to put the dollar sign :Shock: ...anyway.I have 1 question though: Is it necessary for the code to be in a function?

Link to comment
Share on other sites

if you place it below inputs then no...
Are you referring to inline javascript?
Link to comment
Share on other sites

As in

<form action="" method="post">  <input type="radio" class="formBuzType" name="buztype" value="5"> salon spa <br>  <input type="radio" class="formBuzType" name="buztype" value="4"> car <br>		    </form>  <script type="text/javascript">/*<![CDATA[*//*---->*/$('.formBuzType').change(function(){alert('hi');});/*--*//*]]>*/</script>

Where the targetted input have been created, so the event can be applied

Link to comment
Share on other sites

Now, I got it thanks.

Link to comment
Share on other sites

but I have 1 more quesion...not related to this topic...but I am not going to open a new one since probably you are going to answer anywaysince you know js very good. I have a form with 2 radio buttons, and each is associated with a different value.I want every time the user checks a radio button(onchange event) the corresponding value is "submitted". Doing this with PHP is easy but I want to write js code so that ajax can do it.What I have managed so far is that by selecting either buttons the same value is "grabbed"...i mean the one radio button has value 5 and the other 4 but always 5 is "gets " grabbed...here is the code...HTML and js:

<form action="" method="post">  <input type="radio" class="formBuzType" name="buztype" value="5"> salon spa <br>  <input type="radio" class="formBuzType" name="buztype" value="4"> car <br>	    </form><script>$(function(){$('.formBuzType').change(function(){var value=$('.formBuzType').val()alert(value);});});	    </script>	   

Link to comment
Share on other sites

you have to get the value of input that was selected, that is where $(this) is used

$(function(){$('.formBuzType').change(function(){var InputValue = $(this).val();alert(InputValue);});});

this specific element with class .formBuzType that triggered change event

Link to comment
Share on other sites

Is this thread done? I don't want to hijack it, but I would like to ask when it is important to use fancy comment delimiters such as those that are seen below. Thanks.

<script type="text/javascript">/*<![CDATA[*//*---->*/ [...script code...] /*--*//*]]>*/</script>

Edited by davej
Link to comment
Share on other sites

Is this thread done? I don't want to hijack it, but I would like to ask when it is important to use fancy comment delimiters such as those that are seen below. Thanks.
it isn't (anymore)http://w3schools.inv...showtopic=47631 Edited by thescientist
Link to comment
Share on other sites

Since it needs to be valid XML, seeing things like < and & in Javascript code break the XML parsing. The CDATA tells the XML parser that this is character data, not XML code. Then again, I don't see why anyone would use XHTML anyway.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...