March 19, 2012

Setting attributes in jquery

Question by BlackFire27

I have a problem with running jquery:

    <!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        <script type="text/javascript" src="jQuery.js"></script>
      <script type="text/javascript">
       $(function(){

            $('form').attr('id','myNewId');
        });
       </script>
    </head>
    <body>

        <div align="center">
            <form id="myID">
            My Name: <input type="text" name="text"/>
            </form>

        </div>
    </body>
</html>

no error happens , and no intellisense with netbeans..why the code doesnt work ?!?

UPDATE: now it works..

No intellisense with Netbeans..Here is my version:

> Product Version: NetBeans IDE 7.1.1 (Build 201203012225)
Java: 1.7.0_03; Java HotSpot(TM) Client VM 22.1-b02
System: Windows XP version 5.1 running on x86; Cp1255; en_US (nb)
User directory: C:Documents and Settingsdddddddddddd.netbeans7.1.1
Cache directory: C:Documents and Settingsdddddddddddd.netbeans7.1.1varcache

Answer by Starx

jQuery Intellisense is already available on Netbeans. [read more]

It should work the way you are coding [demo], but as of jQuery 1.6+ , you can also use .prop() to set values for attributes[demo]

$('form').prop('id','myNewId');

In case jQuery is not defined, may be it is conflicting with a different jQuery version. Use this at the top of your script

  $.noConflict();

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!