jimfog Posted May 28, 2013 Share Posted May 28, 2013 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 More sharing options...
dsonesuk Posted May 28, 2013 Share Posted May 28, 2013 ?? '$' as in $(function(){$('.formBuzType').change(function(){alert('hi');});}); works for me Link to comment Share on other sites More sharing options...
jimfog Posted May 28, 2013 Author Share Posted May 28, 2013 Ι cannot believe I forgot to put the dollar sign ...anyway.I have 1 question though: Is it necessary for the code to be in a function? Link to comment Share on other sites More sharing options...
dsonesuk Posted May 28, 2013 Share Posted May 28, 2013 depends where you place the code, if you place it below inputs then no, if not then you have use $(function(){ }); which acts similar to $(document).ready(function(){ }); and runs when page is fully loaded. Link to comment Share on other sites More sharing options...
jimfog Posted May 29, 2013 Author Share Posted May 29, 2013 if you place it below inputs then no...Are you referring to inline javascript? Link to comment Share on other sites More sharing options...
dsonesuk Posted May 29, 2013 Share Posted May 29, 2013 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 More sharing options...
jimfog Posted May 29, 2013 Author Share Posted May 29, 2013 Now, I got it thanks. Link to comment Share on other sites More sharing options...
jimfog Posted May 29, 2013 Author Share Posted May 29, 2013 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 More sharing options...
dsonesuk Posted May 29, 2013 Share Posted May 29, 2013 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 More sharing options...
davej Posted May 31, 2013 Share Posted May 31, 2013 (edited) 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 May 31, 2013 by davej Link to comment Share on other sites More sharing options...
thescientist Posted May 31, 2013 Share Posted May 31, 2013 (edited) 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 May 31, 2013 by thescientist Link to comment Share on other sites More sharing options...
justsomeguy Posted May 31, 2013 Share Posted May 31, 2013 You would need the CDATA comments if you're using an XHTML doctype, but it's not needed for HTML5. Link to comment Share on other sites More sharing options...
thescientist Posted May 31, 2013 Share Posted May 31, 2013 really? I didn't know it was an issue. Link to comment Share on other sites More sharing options...
justsomeguy Posted May 31, 2013 Share Posted May 31, 2013 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now