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


#1

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 .


Keyboard Issues
#2

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.


#3

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);
});

});


#4

What is the problem with that code?


#5

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.


#6

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);
	});
});

#7

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?


#8

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.


#9

So what is the problem that needs multiple IDs?


#10

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.


#11

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?


#12

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


#13

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


#14

I don't even know what your code does.


#15

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


#16

Agreed.

Please continue discussion in the original topic here


closed #17