www.webdeveloper.com
Results 1 to 2 of 2

Thread: simple js question from a beginner

  1. #1
    Join Date
    Apr 2013
    Posts
    1

    simple js question from a beginner

    I have the following code for my webpage. It's pure basic but it doesn't do what I want somehow.

    What is should do is, it should change the action target depending on the radio button selection. It doesn't even run the function. Could you please guide me what I am doing wrong?

    Thanks.

    You may find the same code: http://pastebin.com/5jjGQQkA

    Code:
    form id="choose_dept" method="POST" action="http://mywebsite.com/">
    
      
    
      <script type="text/javascript">
        <!--
    
    
    $("#choose_dept").change(function(){
    
      $("#choose_dept").attr('action', 'http://mywebsite.com/' + $('input[name=section]:checked').val() + '/' );
    
      this.submit();
    });
          -->
      </script>
      <div>
        
        <table>
          <tbody>
            <tr>
              <td>
                <input id="section_1" type="radio" name="section" value="tr1">
              </td>
              <td>
                <input id="section_2" type="radio" name="section" value="tr2">
              </td>
              <td>
                <input id="section_3" type="radio" name="section" value="tr3">
              </td>
            </tr>        
          </tbody>
        </table>
      </div>
    </form>

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,654
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Try this</title>
    <link rel="stylesheet" type="text/css" href="css/css.css">
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    </head>
    <body>
    <form id="choose_dept" method="POST" action="http://mywebsite.com/">
    <script type="text/javascript">
    /*$("#choose_dept").change(function(){
    
      $("#choose_dept").attr('action', 'http://mywebsite.com/' + $('input[name=section]:checked').val() + '/' );
    
      this.submit();
    });*/
    $(document).ready(function(){
    $('input[name=section]').each(function(){
    $(this).click(function(){
    $("#choose_dept").attr('action', 'http://mywebsite.com/' + $(this).val() + '/' );
    $("#choose_dept").submit();
    });
    });
    });
    </script>
      <div>
        
        <table>
          <tbody>
            <tr>
              <td>
                <input id="section_1" type="radio" name="section" value="tr1">
              </td>
              <td>
                <input id="section_2" type="radio" name="section" value="tr2">
              </td>
              <td>
                <input id="section_3" type="radio" name="section" value="tr3">
              </td>
            </tr>        
          </tbody>
        </table>
      </div>
    </form>
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles