All posts by Thiyagu

.empty() vs .remove() vs .detach()

jQuery provides various methods to remove elements from DOM. These methods are

  • .empty()
  • .remove() and
  • .detach()

.empty(): This method removes all the child element of the matched element from DOM.

.remove(): This method used when you want to remove the element itself, as well as everything inside it.
(This method takes elements out of the  DOM)

.detach(): This method is the same as .remove(), except that .detach() keeps all jQuery data associated with the removed elements.
detach method is useful in the scenario of removing elements are to be reinserted/added into the DOM at a later time.

Example 1:

HTML :
<div>
<div>HELLO</div>
<div>BYE</div>
</div>

JQUERY :

$(‘.hello’).empty();
$(‘.hello’).remove();

OUTPUT DOM : empty()

<div>
<div></div>
<div>BYE</div>
</div>

OUTPUT DOM : remove()

<div>
<div>BYE</div>
</div>

Exampke 2:

var temp = null;
temp = $(“#dvjQ”).detach();
$(“#dvjQ”).html(temp);

Note : Then detach the div and attach it again  remove also do the same the difference is it also erase the event associated with the element.(hover event….)

.add() Method

.add()

Add elements to the set of matched elements.

HTML:

<p>Hello</p><span>Hello Again</span>

JQUERY:

$(“div”).css(“border”, “2px solid red”)
.add(“p”)
.css(“background”, “yellow”);

This JQ will,

Step : 1  find the div and apply the red to the border
Step : 2  Then add p element to the end of the div and apply background as yellow

OUTPUT :

.find() vs .filter()

Both filter() and find() methods are very similar, except the former is applies to all the elements, while latter searches child elements only.

find() searches the descendants of the elements in the matched set. filter() searches only the elements in the matched set.

To simple

 1.filter() – search through all the elements.
 2.find() – search through all the child elements only.

filter() :

$(‘div’).filter(‘#div1’).css(‘background’,’red’);

find() :

$(‘div’).find(‘#div1’).css(‘background’,’red’);

Difference Between jQuery Text() And HTML()

 

In this post you will learn about the difference between jQuery text and HTML.

.html() – This jQuery function gets/sets the HTML of any element.
.text()- This jQuery function gets/sets the text (innertext) of any element.

Let’s take an example. I have placed a div element, which have a span tag as child and some text.

HTML :

This code given below is the sample HTML document

<div id=”dvFirst”>
<span>jQuery By Example </span>
</div>

Now in .ready() event, both the functions .html() and .text() functions are used on div and the output is placed in alert box.

JQUERY :

$(function () {
alert(“Div HTML is: ” + $(“#dvFirst”).html());
alert(“Div content is: ” + $(“#dvFirst”).text());
});

Now, when you view this page in browser, there will be 2 alerts.

First alert box, will display below text.

Div HTML is: <span>jQuery By Example </span>

And second alert box, will display below text.

Div content is: jQuery By Example

As you see, that the .html() function provides you the inner html and text of the element while text function gives you only text.

.append() VS .html()

.append() and .html() is the most usefull method in jQuery.But these are far different from one another,

.append() add some value with the existing one. whether .html() do the same but it removes the old value

HTML :

<ul id=”test”>
<li>test</li>
</ul>

Now use .append() to add one <li>,

<script type=”text/javascript>”
jQuery(“#test”).append(“<li>test1</li>”);
</script>

The output of this jQuery will be

<ul id=”test”>
<li>test</li>
<li>test1</li>
</ul>

Now if I use .html() to add one <li>, For that I will write

<script type=”text/javascript>”
jQuery(“#test”).html(“<li>test1</li>”);
</script>

The output of this Script will be

<ul id=”test”>
<li>test1</li>
</ul>

Here in this example .append() add one extra <li>, whether .html() removes the old one with new one.
This is the main difference between .append() and .html() in Jquery

Query append() VS appendTo()

Query append() and appendTo()

append() :  Append to end of the selected element

HTML :   <ul id=”test”>
<li>test</li>
</ul>
JQuery :   jQuery(“#test”).append(“<li>test1</li>”);

OUTPUT :  <ul id=”test”>
<li>test</li>
<li>test1</li>
</ul>
appendTo :  Append to end of the selected element

HTML :   <div>Hello</div>

JQuery :  $(‘<p>Test</p>’).appendTo(‘.inner’);

OUTPUT :  <div  class=”inner” > Hello
<p>Test</p>
</div>

Difference:
Both jQuery append() and appendTo() methods are doing the same task, add a text or html
content after the content of the matched elements. The major difference is in the syntax.

//Create our content
var appendHTML = “<p>I’m going to be added to the DOM</p>”;

//Select our element, then add the html
$(“#container”).append(appendHTML);

//Create our content, then select the element to append to
$(appendHTML).appentTo(“#container”);

JQuery Selector

JQUERY SELECTORS :

1) selecting a particular element having a specific class

$(“.class1”).css(“border”, “1px solid red”);

2) select all elements of the page

$(“*”).css(“border”, “1px solid red”);

3) select an element that having a particular id

$(“#p1”).css(“border”, “1px solid red”);

4) select all specific elements type

$(“p”).css(“border”, “1px solid red”);

5) select multiple elements in a single selector

$(“div.class1, #div1”).css(“border”, “1px solid red”);

Note : From above the selector first select div having the class name with
            “.class1” and select element having id as “div1”  

 6) select a specific child of the parent element

$(“#div2 p:nth-child(2)”).css(“border”, “1px solid red”);

Note : select the 2nd paragraph (p) element that is inside the div
             element whose id is “div2” and apply the css on the element

7) select last child of the parent element

$(“#div2 p:last-child”).css(“border”, “1px solid red”);

8) select the first child of the parent element

$(“#div2 p:first-child”).css(“background”, “red”);

Note : select the first paragraph (p) element which is inside the
            div element whose id is “div2” and  apply the css on the element.

9) select an element based on its attribute

$(‘input[id$=”txtAddress”]’).val(‘Testing’);

Note : select the textboxes having id ending with “txtAddress” and set its value as “Testing”.

Issue While exporting Gridview to Excel while deployment

Issue While exporting Gridview to Excel

 Issue    : Image is not showing in the Excel

 Solution : The URL must be

ImageUrl=”http://localhost:63360/Website/Images/help.gif” /> instead of ImageUrl=”~/help.gif”.

 So place URL in webcofig

<add key=”ImagePath” value=”http://localhost:63360/Website/Images/”/>

CodeBind :

imgStat.ImageUrl = ConfigurationSettings.AppSettings[“ImagePath”].ToString() + @”help.png”;

find the div inside the Gridview

How to find the div inside the Gridview (outside of gridview function)

.aspx file

<div id=”tooltip” style=”display: none;”></div>

Code Behind file

Make hide of the div

foreach (GridViewRow row in gvdStatus.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{

HtmlGenericControl dive = new HtmlGenericControl();
dive = ((HtmlGenericControl)(row.FindControl(“tooltip”)));
dive.Visible = false;
}
}

Applying color to the row based on the condition

if (e.Row.RowType == DataControlRowType.DataRow)
{
if (Convert.ToInt32(e.Row.Cells

[2].Text) > 525)
{
e.Row.BackColor = System.Drawing.Color.Blue;
}
}

Note :

A table can have header data or footer rows. The above statement will make sure that the row is data row.
row.RowType == DataControlRowType.DataRow: use it every time if you want to look for something in every row.