9 Replies - 548 Views - Last Post: 30 March 2015 - 09:12 AM

#1 CY5  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 413
  • Joined: 28-September 12

Attach event handler to table

Posted 30 March 2015 - 04:11 AM

Suppose i have table with textbox inside every cell and i want to check whether all textbox inside table are filled or not, if they are then alert me done. so i was thinking about making a function which checks all textbox inside table and attach it to setInterval so that it checks every interval of time but i think it is inefficient so is there any other way out ?
Is This A Good Question/Topic? 0
  • +

Replies To: Attach event handler to table

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4138
  • View blog
  • Posts: 13,074
  • Joined: 08-June 10

Re: Attach event handler to table

Posted 30 March 2015 - 05:52 AM

on which occasions should the text boxes be (un)filled? essentially it would suffice to hook into the events that occur before you require all text boxes to be filled.
Was This Post Helpful? 1
  • +
  • -

#3 CY5  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 413
  • Joined: 28-September 12

Re: Attach event handler to table

Posted 30 March 2015 - 07:35 AM

for example :
All the cells inside tables are not filled when the page is loaded but as user fills all the cells either with text or number it should alert "table filled"
note:inside every cell there is textbox

I think here you will get my point I want to raise the event when the table is filled with inputs or else do nothing.
using setInterval is my last option to check every second whether table is filled or not

This post has been edited by CY5: 30 March 2015 - 07:45 AM

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4138
  • View blog
  • Posts: 13,074
  • Joined: 08-June 10

Re: Attach event handler to table

Posted 30 March 2015 - 07:43 AM

hook into the text boxes’ change event and alert if you find every box filled.
Was This Post Helpful? 1
  • +
  • -

#5 CY5  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 413
  • Joined: 28-September 12

Re: Attach event handler to table

Posted 30 March 2015 - 08:06 AM

well i have textboxes with same class name so will this do
jQuery('.b_txt').on("change",function(){
          alert("table filled");
});


Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4138
  • View blog
  • Posts: 13,074
  • Joined: 08-June 10

Re: Attach event handler to table

Posted 30 March 2015 - 08:08 AM

this will show the alert on every change.

you need to check first if every box has content in it.
Was This Post Helpful? 1
  • +
  • -

#7 CY5  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 413
  • Joined: 28-September 12

Re: Attach event handler to table

Posted 30 March 2015 - 08:14 AM

that's my question i will have to check content of every textbox every interval and if its filled alert. I want to avoid interval

which is correct way to check textbox with empty string ?
var text_box=jQuery(this).find('.b_txt').val();
	    		if(text_box===''){
	    			return false;
	    		}


OR
var text_box=jQuery(this).find('.b_txt').val();
	    		if(text_box.length>0){
	    			return false;
	    		}


OR
var text_box=jQuery(this).find('.b_txt').val();
	    		if(!text_box){
	    			return false;
	    		}


This post has been edited by CY5: 30 March 2015 - 08:29 AM

Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4138
  • View blog
  • Posts: 13,074
  • Joined: 08-June 10

Re: Attach event handler to table

Posted 30 March 2015 - 08:49 AM

Quote

I want to avoid interval

by hooking into the events, you avoid the interval.

Quote

which is correct way to check textbox with empty string ?

neither.

I’d go for
var all_filled = !Array.prototype.filter.call(document.querySelectorAll('.b_txt'), function (box) {
    return box.value.trim().length === 0;
}).length;



As I see, there is also such an equivalent in jQuery.

var all_filled = !$('.b_txt').filter(function () {
    return this.value.trim().length === 0;
}).length;

Was This Post Helpful? 1
  • +
  • -

#9 CY5  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 413
  • Joined: 28-September 12

Re: Attach event handler to table

Posted 30 March 2015 - 08:51 AM

Quote

I’d go for
var all_filled = !Array.prototype.filter.call(document.querySelectorAll('.b_txt'), function (box) {
    return box.value.trim().length === 0;
}).length;



As I see, there is also such an equivalent in jQuery.

var all_filled = !$('.b_txt').filter(function () {
    return this.value.trim().length === 0;
}).length;

The above code will check empty string in all textboxes of class name?

thanks @Dormilich its checking all the text boxes with class name

This post has been edited by CY5: 30 March 2015 - 09:00 AM

Was This Post Helpful? 0
  • +
  • -

#10 CY5  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 413
  • Joined: 28-September 12

Re: Attach event handler to table

Posted 30 March 2015 - 09:11 AM

your code was very helpful thanks once again i attached it to keyup event and its working
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1