Add and Remove ROWS in table using JQuery

Category : JQuery/JScript

Sounds hard, but very simple.

Say I have a table on a page and a button to add rows to that table. On that button, I have added an ONCLICK to go to the function below.

Below, I gave my table a CLASS name because .Net screws up the ID — and I am able to call that when the button is clicked.
Here the .append function adds after the last element — THIS COULD BE PROBLEM FOR YOU if you have a TBODY. There are workarounds but I am not going to discuss those at this time.

function AddCustomSetting() {
    $('.cusSettingsTable').append("<tr><td onclick='RemoveRow(this);'>Remove</a></td><td><input type='textbox' /></td><td>Test Me</td><td></td></tr>");
}

That is all it would take to add a new Row to a table… now what if we want to remove it? Well notice in that append statement that I added another onclick function to remove the row and sent the TD (via this) element with the call. Here is that function:

function RemoveRow(c) {
    $(c).parent().remove();
}

And that is it! JQuery strikes again!
ciao!
t

Add JQuery, Javascript, CSS to ASPx User Controls parent

Category : ASP.Net, C#, JQuery/JScript

This is my preferred method. (Microsoft has controls such as ScriptManager and ScriptManagerProxy but like nearly everything they do, I believe its overkill) To me, this is the simplest method.

The Problem: You want to convert or to create a user control that has scripts and/or css.

Solution: Within the Page_Load event of the user control, simply include the following:

    //For Scripts - notice we add this script call to the PARENT HEADER
    LiteralControl jQuery = new LiteralControl();
    jQuery.Text = "<script type=\"text/javascript\" src=\"js/jquery.js\"></script>";
    Page.Header.Controls.Add(jQuery);
 
    //For CSS - You could add Media under attributes as well.  
    HtmlLink stylesLink = new HtmlLink();
    stylesLink.Attributes["rel"] = "stylesheet";
    stylesLink.Attributes["type"] = "text/css";
    stylesLink.Href = "css/yourPagesCSS.css";
    Page.Header.Controls.Add(stylesLink);

Hope this helps someone! t

Trying to get the divs within a div by class name? jquery

Category : JQuery/JScript

$('#DIVID &gt; div.CLASSNAME').each(function() {
           numOfColumns++;
                });

This asks for all divs within your holding div that have a certain class name. ** THE ‘ATGT’ after DIVID is a Greater Than sign**

How to tell if a div is visible / hidden using JQuery

Category : JQuery/JScript

You have a DIV and want to check if it is visible

Use the is() function passing “:visible” :

if( $('#myDiv').is(':visible') ) {}

Hidden

This is the same as checking if an element is visible but uses :hidden instead and the logic is the reverse:

if( $('#myDiv').is(':hidden') ) {}


Elements will return TRUE to the Hidden check even if they are not but take up no space!

Make the IS NOT check to combat this.

if( !$('#myDiv').is(':visible') {}
}

You can use the visible parameter to loop through all visible divs

$("#mydiv div:visible").each( function() {}

Change your javascript variables in your event tag.

Category : JQuery/JScript

fyi – you can change one of your javascript variables on your javascript event tag!

For instance, I wanted to simply change a variable when someone went in and then out of the username text box… i didnt want to write a function JUST to change a variable so i tried this and it worked!
onblur=”g_uNameChanged=true”

OH YA,,,,, I also found out that you can actually do two javascript things on the same line, because I checked the variable like this:

onblur=”g_uNameChanged=true; alert(g_uNameChanged);”

cheers!

t

Get CharCode / KeyCode in one brilliant JQuery line. (w/ CHARCODE TABLE)

Category : JQuery/JScript

Have I mentioned that I love JQuery? Well I do, and I have…but I’m saying it again… I DO.

Here is a way to grab a keyCode (firefox) or CharCode (ie) with just couple total lines of code.

Say you make a textbox with an id of ‘tester’, the following code will give you the key(char)code of whatever is pressed WHEN it is pressed inside the textbox.

     $('#tester').bind("keydown", function(e) {
                var key = e.charCode || e.keyCode || 0;
                alert(key);
            });

NOTICE: var key = e.charCode || e.keyCode || 0;

This is a very nice way of grabbing the key code depending on what browser you are using.

Very nice snippet, thanks Javascript gurus!!!
t
======

AND BTW — Here is the Charcode table:

Char
Key Name
8 Backspace
9 Tab
13 Enter
16 Shift
17 Ctrl
18 Alt
19 Pause
20 Caps Lock
27 Escape
33 Page Up
34 Page Down
35 End
36 Home
37 Arrow Left
38 Arrow Up
39 Arrow Right
40 Arrow Down
45 Insert
46 Delete
48 0
49 1
50 2
51 3
52 4
53 5
54 6
55 7
56 8
57 9
65 A
66 B
67 C
68 D
69 E
70 F
71 G
72 H
73 I
74 J
75 K
76 L
77 M
78 N
79 O
80 P
81 Q
82 R
83 S
84 T
85 U
86 V
87 W
88 X
89 Y
90 Z
91 Left Windows
92 Right Windows
93 Context Menu
96 NumPad 0
97 NumPad 1
98 NumPad 2
99 NumPad 3
100 NumPad 4
101 NumPad 5
102 NumPad 6
103 NumPad 7
104 NumPad 8
105 NumPad 9
106 NumPad *
107 NumPad +
109 NumPad -
110 NumPad .
111 NumPad /
112 F1
113 F2
114 F3
115 F4
116 F5
117 F6
118 F7
119 F8
120 F9
121 F10
122 F11
123 F12
144 Num Lock
145 Scroll Lock
186 ;
187 =
188 ,
189 -
190 .
191 /
192 `
219 [
220 \
221 ]
222

Custom JQuery Selectors

Category : JQuery/JScript

:even Selects every other (even) element from the matched element set.

:o dd Selects every other (odd) element from the matched element set.

:eq(0) and :nth(0) Selects the Nth element from the matched element set

:gt(N) Selects all matched elements whose index is greater than N.

:lt(N) Selects all matched elements whose index is less than N.

:first Equivalent to :eq(0)

:last Selects the last matched element.

:parent Selects all elements which have child elements (including text).

:contains(‘test’) Selects all elements which contain the specified text.

:visible Selects all visible elements (this includes items that have a display of block or inline, a visibility of visible, and aren’t form elements of type hidden)

:hidden Selects all hidden elements (this includes items that have a display of none, or a visibility of hidden, or are form elements of type hidden)

Jquery Css Selector solving .Net problems!

Category : JQuery/JScript

So working with a custom control and having trouble manipulating a couple input boxes in that control on the page that the control lives in.

So I had a custom control embedded on a page with two calendar input boxes. I wanted to check on the submit button to that the end date was after the start date. I could not get this to work in the control.

But I was able to give those text boxes class names (ie: cssClass=”DatesTB”) and on the page that the control resided in I simply did this on the submit button click (in html) ….

 if ($("input.DatesTB:first").val() > $("input.DatesTB:last").val()) {
            writeMessageToPage("The End Date must be after the Start Date.");
            return false;
 
        }

Notice how you can cooly grab the FIRST input box with a specific class name by the JQuery selector
$(“input.CLASSNAME:first) and subsequently get the last one with the “last” identifier!

Very nice and very helpful JQUERY! I love you!!!!

You can also do something like this $(“td:eq(2)”) to get a certain number of element on your page.

That example would be grabbing the 3rd TD on the page.

Love it, hope it helps!

JQuery Radio buttons – get value

Category : JQuery/JScript

jQuery 1.3 and above

  1. var var_name = $(“input[name='radio_name']:checked”).val();

jQuery 1.2

  1. var var_name = $(“input[@name='radio_name']:checked”).val();

How to make .setAttribute(‘disabled’, true) work in FireFox…why JQUERY > JScript

Category : JQuery/JScript

OK, there are alot of reasons why JQuery is nice to use OVER Javascript… (shorter syntax…cool stuff in very little code, etc…)… AND YES EVERYONE (my co-workers included) I know that JQUERY IS Javascript…

However, the gurus who created JQuery have apparently found a fix to a firefox/javascript problem that Mozilla has not wanted to address and because of that … JQUERY is officially better than Javascript!

With that out of the way… let me explain the situation…

We were trying to disable a checkbox based simply upon another checkbox being checked… it looked something like this:

if(document.getElementById(‘roleStateAdmin’).getAttribute(‘checked’)) {
document.getElementById(‘roleTechAdmin’).setAttribute(‘checked’,  false);
document.getElementById(‘roleTechAdmin’).setAttribute(‘disabled’, true);
}
else if(document.getElementById(‘roleTechAdmin’).getAttribute(‘checked’)) {
document.getElementById(‘roleStateAdmin’).setAttribute(‘checked’,  false);
document.getElementById(‘roleStateAdmin’).setAttribute(‘disabled’, true);
}
else {  // Neither checked.
document.getElementById(‘roleStateAdmin’).setAttribute(‘disabled’, false);
document.getElementById(‘roleTechAdmin’).setAttribute(‘disabled’, false);

This code worked just as intended in I.E.  but completely broke in FF. After several Google searches to find the problem, I officially chocked it up to being Broken in FF and decided that my only option was to see if JQuery could make it work.

It COULD and did… here it is the ALTERNATE Jquery code that makes this action work just fine:

if($(‘#roleStateAdmin’).attr(‘checked’)) {
$(‘#roleTechAdmin’).attr(‘checked’,  false);
$(‘#roleTechAdmin’).attr(‘disabled’, true);
}
else if($(‘#roleTechAdmin’).attr(‘checked’)) {
$(‘#roleStateAdmin’).attr(‘checked’,  false);
$(‘#roleStateAdmin’).attr(‘disabled’, true);
}
else {  // Neither checked.
$(‘#roleStateAdmin’).attr(‘disabled’, false);
$(‘#roleTechAdmin’).attr(‘disabled’, false);