How to get id of every form all element on focus through jquery?

It is for first input box
var $write = $(‘input[type=text]:first’).focus(function() {
but i want when i focus any input box so its id store in $write function.
like first input box id=“item1” so when focus on input box so storing item1
then if input box id=“item2” so when focus on second input box so storing item2 .

First, $write is not a function, it’s the selected jQuery element (in this case the first text input).

The ID of the focused input is available inside the event handler callback either via this.id or the event target element.

yes sir write is not function. how we use it?

here is my code

    var ids =  $(':input').bind('focus', function(){
	  console.log($(this).attr('id')); // gets the id of a clicked link that has a class of menu
	  //console.log('ids');
     });	
      $(function(){		
      var $write = ids,
	shift = false,
	capslock = false;

$('#keyboard li').click(function(){
	var $this = $(this),
		character = $this.html(); // If it's a lowercase letter, nothing happens to this variable
	
	// Shift keys
	if ($this.hasClass('left-shift') || $this.hasClass('right-shift')) {
		$('.letter').toggleClass('uppercase');
		$('.symbol span').toggle();
		
		shift = (shift === true) ? false : true;
		capslock = false;
		return false;
	    }
	
	// Caps lock
	if ($this.hasClass('capslock')) {
		$('.letter').toggleClass('uppercase');
		capslock = true;
		return false;
	}
	
	// Delete
		if ($this.hasClass('delete')) {
			var html = $write.html(),
			txt = html.substr(0, html.length - 1);
		  $write.html(txt);
		  $write.autocomplete("search", txt);
		  return false;
		}
	
	// Special characters
	if ($this.hasClass('symbol')) character = $('span:visible', $this).html();
	if ($this.hasClass('space')) character = ' ';
	if ($this.hasClass('tab')) character = "\t";
	if ($this.hasClass('return')) character = "\n";
	
	// Uppercase letter
	if ($this.hasClass('uppercase')) character = character.toUpperCase();
	
	// Remove shift once a key is clicked.
	if (shift === true) {
		$('.symbol span').toggle();
		if (capslock === false) $('.letter').toggleClass('uppercase');
		
		shift = false;
	}
	
	// Add the character
	$write.val($write.val() + character);		
	$write.html(txt);
	$write.autocomplete("search", txt);
});

});

What is the problem with that code?

I’m gonna guess… Variable Undefined: txt
(Txt is only conditionally defined by the Delete section, and is invariably invoked by the add the character section.

Dear sir i am sorry
But In this code i am using two function
in one i get id and second i want to store ids in $write = ids,How we use it
Here is my code

     $(':input').bind('focus', function(){
                   var ids = ($(this).attr('id')); // gets the id of a clicked link that has a class of menu
		//console.log('ids');
     });	
       $(function(){		
	var $write = ids,
		shift = false,
		capslock = false;
	
	$('#keyboard li').click(function(){
		var $this = $(this),
			character = $this.html(); // If it's a lowercase letter, nothing happens to this variable
		
		// Shift keys
		if ($this.hasClass('left-shift') || $this.hasClass('right-shift')) {
			$('.letter').toggleClass('uppercase');
			$('.symbol span').toggle();
			
			shift = (shift === true) ? false : true;
			capslock = false;
			return false;
		}
		
		// Caps lock
		if ($this.hasClass('capslock')) {
			$('.letter').toggleClass('uppercase');
			capslock = true;
			return false;
		}
		
		// Delete
			if ($this.hasClass('delete')) {
				var html = $write.html(),
				txt = html.substr(0, html.length - 1);
			  $write.html(txt);
			  $write.autocomplete("search", txt);
			  return false;
			}
		
		// Special characters
		if ($this.hasClass('symbol')) character = $('span:visible', $this).html();
		if ($this.hasClass('space')) character = ' ';
		if ($this.hasClass('tab')) character = "\t";
		if ($this.hasClass('return')) character = "\n";
		
		// Uppercase letter
		if ($this.hasClass('uppercase')) character = character.toUpperCase();
		
		// Remove shift once a key is clicked.
		if (shift === true) {
			$('.symbol span').toggle();
			if (capslock === false) $('.letter').toggleClass('uppercase');
			
			shift = false;
		}
		
		// Add the character
		$write.val($write.val() + character);		
		$write.html(txt);
		$write.autocomplete("search", txt);
	});
});

No, you don’t. $write is supposed to be a jQuery element, not a list of ids. (that’s why the line var $write = ids, breaks your script)

What do you want to do with those IDs anyways?

because sir,
actually sir in first function i get id when i use my cursor i get id from it. and then when its store in var ids= ($(this).attr(‘id’)); ids have id when cursor in input box so after i do $write = ids .so direct i get one id at one time.

So what is the problem that needs multiple IDs?

Actually in my keyboard plugin its feature for only one input box, because its take id of only one input box and write easily its good to use but when we use multiple input box so $write take one id at a time and when focus in input box that’s why i need multiple Id.

You can reassign $write to another jQuery object on focus as suggested in your other thread, so you don’t even need IDs here. Did you give that a try?

Wouldn’t it make sense to write a real jQuery plugin then?

How i use It can you explain it. Can you make snippet from my code and its work in multiple input box.

I don’t even know what your code does.

Maybe we should continue commenting to your original topic to avoid confusion.

Agreed.

Please continue discussion in the original topic here

1 Like