16

I was given this chunk of code to place in my html page but I know nothing about javascript so I have no idea where to place it and what kind of tag to place it in.

$('input[type=radio]').change(function() {

$('input[type=radio]').each(function(index) {
  $(this).closest('tr').removeClass('selected');
});

$(this).closest('tr').addClass('selected');
});
bjb568
  • 11,089
  • 11
  • 50
  • 71
user1888584
  • 229
  • 1
  • 3
  • 8
  • 2
    place it within script tags `` – Ayush Dec 11 '12 at 22:38
  • 2
    BTW, this is not pure javascript. You need to get jQuery libraries and include them on the page. Google for jQuery. – Kaf Dec 11 '12 at 22:39
  • 1
    You'll also need to make sure the jquery library is added as well. See this post: http://stackoverflow.com/questions/441412/is-there-a-link-to-the-latest-jquery-library-on-google-apis – Kai Qing Dec 11 '12 at 22:40

6 Answers6

26

Including the jQuery Library

That is jQuery code. You'll first need to make sure the jQuery library is loaded. If you don't host the library file yourself, you can hotlink one from the jQuery CDN:

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>

You can do this within the <head> section, but it's fine as long as it's loaded before your jQuery code.

Further reading:


Placing Your Code in the Page

Place your code inside <script> tags. It can be inserted anywhere within either <head> or <body>. If you place it before the <input> and <tr> tags (as referenced in your code), you have to use $(document).ready() to make sure those elements are present before the code is run:

$(document).ready(function() {
    // put your jQuery code here.
});

If you want your page content to be loaded as soon as possible, you might want to place it as close as the </body> close tag as possible. But another common practice is to place all JavaScript code in the <head> section. This is your choice, based on your coding style and needs.

Suggestion: Instead of embedding JS/jQuery code directly into an HTML page, consider placing the code in a separate .js file. This will allow you to reuse the same code on other pages:

<script src="/path/to/your/code.js"></script>

Further reading:

Community
  • 1
  • 1
Andrew
  • 2,770
  • 1
  • 22
  • 29
  • 2 other CDN for Javascript libraries: [cdnjs.com](http://www.cdnjs.com/) and [jsdelivr.com](http://www.jsdelivr.com/) – Adriano Jun 20 '14 at 09:00
5

You need to wrap this in script tags:

<script type='text/javascript'> ... your code ... </script>

That being said, it's important WHEN you execute this code. If you put this in the page BEFORE the HTML elements that it is hooking into then the script will run BEFORE the HTML is actually rendered in the page, so it will fail.

It is common practice to wrap this type of code in a "document ready" block, like so:

<script type='text/javascript'>
$(document).ready(function() {

... your code...

}}
</script>

This ensures that the entire page has rendered in the browser BEFORE your code is executed. It is also a best practice to put the code in the <head> section of your page.

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
  • tried it but doesn't work. can you do a view source on this page: http://www.amolito.com/index.php?id=4. Also, maybe this may help, I am trying to change the background color of the selected radio when selected. I was told the script would work but maybe the other person is wrong? – user1888584 Dec 11 '12 at 23:06
  • @user1888584 You need to include jQuery **before** the your code is run. – Andrew Dec 11 '12 at 23:12
  • if you scroll down a little you'll see the jQuery – user1888584 Dec 11 '12 at 23:26
  • @user1888584 Yes, I saw it. And that's why I bolded the word **before**. That line needs to go **before** your `$(document).ready` code. Also, have a look at the console messages. You have a typo on line 35. The second closing curly bracket `}` should be a parenthesis `)`. It should be `})`, not `}}`. – Andrew Dec 12 '12 at 02:51
  • checked again and its only working partially.. if you go to http://www.amolito.com/index.php?id=4 you'll see that both radio show the class selected. if you select the second button, it works as should. do you know how to fix it so that on default it only show the background of one? thanks – user1888584 Dec 12 '12 at 07:24
  • @user1888584 Both `` tags have the `selected` class when the page is loaded. You probably have to adjust your PHP code to only give the `selected` class to one of the rows only. – Andrew Dec 12 '12 at 09:49
  • tried copying my php but it exceeds the limit.. not sure how to post code. can you let me know how to post code? – user1888584 Dec 16 '12 at 04:43
  • @user1888584 Have you tried something like [Pastebin](http://pastebin.com/)? – Andrew Dec 16 '12 at 11:47
  • @andrewap Here's the php http://pastebin.com/ckbE7uZb can you help me look through it to see what went wrong? thanks – user1888584 Dec 18 '12 at 04:44
  • @user1888584 It's a bit challenging to understand the code without knowing what PHP framework you're using. Nonetheless, the logic in line 44 causes `$selected` to always have the value `'selected'` when `$idx == 1` (a.k.a. 1st method). This is what's causing both methods to be highlighted when the 2nd method is selected. I suggest rewriting that logic to first consider whether there's a selected value. Only default it to the first method if there isn't a selected value. Also, this is not related to your original question. You should post this as a separate question on Stack Overflow. – Andrew Dec 18 '12 at 09:20
  • In HTML5, you don't need to add the `type='text/javascript'` – Brandroid Mar 31 '14 at 04:16
1

Inside of your <head></head> tags add...

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $('input[type=radio]').change(function() {

    $('input[type=radio]').each(function(index) {
        $(this).closest('tr').removeClass('selected');
    });

        $(this).closest('tr').addClass('selected');
    });
});
</script>

EDIT: The placement inside of <head></head> is not the only option...this could just as easily be placed RIGHT before the closing </body> tag. I generally try and place my JavaScript inside of head for placement reasons, but it can in some cases slow down page rendering so some will recommend the latter approach (before closing body).

Jared
  • 5,840
  • 5
  • 49
  • 83
0

You can include JQuery using any of the following:

  • Link Using jQuery with a CDN

<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>

  • Download Jquery From Here and include in your project
  • Download latest version using this link

Your code placement can look something like this

  • Your Jquery should be included before using it any where else it will throw an error

```

<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
$(document).ready(function(){
    $('input[type=radio]').change(function() {

    $('input[type=radio]').each(function(index) {
        $(this).closest('tr').removeClass('selected');
    });

        $(this).closest('tr').addClass('selected');
    });
});
</script>

```

Abdul Moiz Khan
  • 693
  • 5
  • 13
-1

Include javascript using script tags just before your ending body tag. Preferably you will want to put it in a separate file and link to it to keep things a little more organized and easier to read. Theres a simple article here that will show you how http://www.selftaughtweb.com/how-to-include-javascript/

suprob
  • 1
-5

You need this code wrap in tags and put on the end of page. Or create JS file (for example test.js), write this code on it and put on the end of page this tag