tags:

views:

32

answers:

2

How come this code will work perfectly fine:

<script>
$('.changeStuff').click(
function() {
  $('#number2').val(12.00);
});
</script>

<body>
<h2 class="changeStuff">Lets change some stuff</h2>

<input name="number2" id="number2" value="11.00"> 
</body>

While this code, written to be used in an onClick event, wont work at all?

<script>
function change_stuff(b) {
  $('#number2').val(b);
}
</script>

<body>
<h2 onclick="change_stuff('12.00')">Lets change some stuff</h2>

<input name="number2" id="number2" value="11.00"> 
</body>
A: 

It works for me. http://jsbin.com/ajuko3

That said, I'm not sure why you'd prefer the second method over the first.

Matt Ball
+1  A: 

i think the h2 element dosnt have an onclick event in all browsers.

thats why the second way might work. but not an all browsers.

but.. when you run a jquery selector on the h2 element the jquery library "wrapped" the element giving it all of the jquery events and function.

and then because the jquery object has a click event and jQuery works an all browsers that way worked for you.

so thanks agian to jquery! for makeing our lives easier.

guy schaller