Jquery and parameter-passing: passing the string value into functions

Hi,

I want to pass the string value which I get from the textarea into the focus() and blur(),
but why what I will get is [object object]?

I get the string value of each textarea successfully with each() and I store the string in a var -

`var value_default = $this.val();`

Then I pass this var into

`$this.focus(function (value_default) {..})`
  • am I passing it incorrectly?

If I alert this value_default I will get [object oject] -

alert(value_default);

I know I can pass the parameter just like this,

.focus(function(){

and

.blur(function(){

but I will get ‘error on page’ on my IE7 with this. also I must not set the var to global bcos I have other textareas with different default text values…

Below is the entire code if you need to see it…

$(".autohide").each(function(){
	
		/* set the variable and store its value */
		var $this = $(this);
		
		var value_default = $this.val();
		
		var parent_autohide = $this.parents('form');
		
		var parent_button = $('input[type=submit]',parent_autohide).parents("div:.item-form").hide();
		
		
		var parent_marginbottom = parseInt($this.parents("div:.item-form").css("marginBottom"));
		
		$this.parents("div:.item-form").css({margin:'0px'});
		
		alert(value_default); // I will get the string value of each textarea element

		$this.focus(function (value_default) {

			alert(value_default); // I will get [object object]
			
			var $this = $(this);
			
			$this.elastic();
			
			$this.parents('div:.item-form').css({margin:'0px 0px '+parent_marginbottom+'px 0px'});
			
			var $this_parent = $this.parents('form');
			
			var $this_button =  $('input[type=submit]',$this_parent).parents("div:.item-form").show();


		}).blur(function(value_default){
		
			alert(value_default); // I will get [object object]
				
			var $this = $(this);

			var value_current = $this.val();
			
			if ( value_current == value_default)
			{
				$this.css({ height: 'inherit'});
				
				$this.parents('div:.item-form').css({margin:'0px'});
				
				var $this_parent = $this.parents('form');
				
				var $this_button =  $('input[type=submit]',$this_parent).parents("div:.item-form").hide();
				
			}
		});
	
	});

many thanks.

… , There is something wrong.
Do you read the jQuery API?

in jQuery,
‘jQuery().focus(fn)’ is binded a event handler on the element focus event.

fn is a event handler function with one parameter and the parameter is event(be formatted by jQuery)
so the function’s parameter is send by the event trigger. wasn’t your defined before.

do i say clearly?

thank you. now I know what was causing the error on IE -

$this.css({height: 'inherit'}); // this line will create error on IE

$this.css({height:''}); // IE likes this

so what is wrong with ‘inherit’ that IE won’t take it like other browsers!??

so you should write $this.focus(function () { });

if it’s a click event ,you need the event infomation.you can write like this.

$this.click(funtion(event){
var x = event.x;
var y = event.y;
})

or you want to get some value with the event object,you can do like this.

$this.bind(‘click’,“Here is sth.”,funtion(event){
alert(event.data);
},)

IE6,7 doesn’t support the ‘inherit’ value…

IE8 or later maybe support. i don’t try.

now I see… thanks so much!

thank you very much for this explanation :slight_smile: