tags:

views:

28

answers:

2

I have written very simple script with jQuery, but the jquery code not working in firefox:

<a href="" class="po">any text</a>
<form>
    <input type="text" class="infobox"/>
    <br />
    <textarea class="me"></textarea>
    <input type="submit"   value="click"  class="submit"/>
</form>

jQuery

var vl = $('.po').text();
$('.po').click(function(){
    $('.me').val(vl);
});

Why this script doesnot work in firefox? Thanks in advance

+1  A: 

$(".po") is a link, so you should use event.preventDefault() or return false so you don't navigate away from the page when you click it.

Also make sure you are using script tags

<a href="" class="po">any text</a>
<form>
<input type="text" class="infobox"/>
<br />
<textarea class="me"></textarea>
<input type="submit"   value="click"  class="submit"/>
</form>
<script type="text/javascript">
var vl = $('.po').text();
$('.po').click(function(event){
    $('.me').val(vl);
    event.preventDefault();
});
</script>

Try it out with this jsFiddle

Or, you can put the JS in the head like this:

<html>
<head>
<script type="text/javascript">
$(function() {                                                   \\ <== doc ready
    var vl = $('.po').text();
    $('.po').click(function(event){
        $('.me').val(vl);
        event.preventDefault();
    });
});
</script>
</head>
<body>
<a href="" class="po">any text</a>
<form>
<input type="text" class="infobox"/>
<br />
<textarea class="me"></textarea>
<input type="submit"   value="click"  class="submit"/>
</form>
</body>
</html>

Try it out with this jsFiddle

Peter Ajtai
@peter, your answer solved my prolem but I cant check this in 9 minutes!Thank you very much
phpExe
@phpExe - You're welcome ;)
Peter Ajtai
+1  A: 

Your code seems to be fine, try putting it in ready handler:

<script type="text/javascript">
$(function(){
  var vl = $('.po').text();
  $('.po').click(function(){
      $('.me').val(vl);
      return false;
  });
});
</script>
Sarfraz
The `return false` that you added in is also essential. Without it, the text gets added, but then the page refreshes, so the original code in the OP isn't really functional in a meaningful way.
Peter Ajtai
@Peter Ajtai: True and that explains the very existence of it there :)
Sarfraz