Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jun 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Accessing Form Data jquery and javascript differences

    I'm new to dynamic forms. Is there a difference in accessing form elements between javascript and jquery?

    For example, I heard the <input> and <textarea> tags for javascript use the "name" or "id" attribute -- which one? Is it the same for jquery?

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    The differ but not totally.

    In JS, you can access an elements ID via id, name or class attribute.

    • In JS, to access an element with ID is:
      Code:
      <script type="text/javascript">
      window.onload=function(){
      document.getElementById('myid').value='I see some trash.';
      }
      </script>
      <input type="text" id="myid">
      ...while in jquery, it's:
      Code:
      <script type="text/javascript" src="jquery.js"></script>
      <script type="text/javascript">
      $(document).ready(function(){$('#myid').value='I see some trash.';});
      </script>
      <input type="text" id="myid">
    • In JS, you can access an element using the class attribute by:
      Code:
      <script type="text/javascript">
      window.onload=function(){
      for(var i=0,myid=document.getElementsByTagName('input');i<myid.length;i++)
      	if(myid[i].className.toLowerCase()=='myid')
      		myid[i].value='I see some trash.';
      }
      </script>
      <input type="text" class="myid">
      ...while using jquery is:
      Code:
      <script type="text/javascript" src="jquery.js"></script>
      <script type="text/javascript">
      $(document).ready(function(){$('.myid').value='I see some trash.';});
      </script>
      <input type="text" class="myid">
    • To access the name attribute via JS could be:
      Code:
      <script type="text/javascript">
      window.onload=function(){
      document.getElementsByName('myid')[0].value='I see some trash.';
      }
      </script>
      <input type="text" name="myid">
      or
      Code:
      <script type="text/javascript">
      window.onload=function(){
      document.myform.myid.value='I see some trash.';
      }
      </script>
      <form name="myform">
      <input type="text" name="myid">
      </form>
      ...while using jquery is:
      Code:
      <script type="text/javascript" src="jquery.js"></script>
      <script type="text/javascript">
      $(document).ready(function(){$("input[name='myid']").val('I see some trash.');});
      </script>
      <input type="text" name="myid">


    For further reading:
    http://docs.jquery.com/Selectors

    Hope that makes sense.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph


  •  

    Posting Permissions

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