a developer's notes – a semi-technical web development BLOG

January 8, 2014

How to add / remove textbox dynamically with jQuery

Filed under: Javascript / JQuery — Duy Nguyen @ 10:00 pm
Tags: , , , , , , ,

This is a great tutorial!
http://www.mkyong.com/jquery/how-to-add-remove-textbox-dynamically-with-jquery/

<html>
<head>
<title>jQuery add / remove textbox example</title>
 
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
 
<style type="text/css">
	div{
		padding:8px;
	}
</style>
 
</head>
 
<body>
 
<h1>jQuery add / remove textbox example</h1>
 
<script type="text/javascript">
 
$(document).ready(function(){
 
    var counter = 2;
 
    $("#addButton").click(function () {
 
	if(counter>10){
            alert("Only 10 textboxes allow");
            return false;
	}   
 
	var newTextBoxDiv = $(document.createElement('div'))
	     .attr("id", 'TextBoxDiv' + counter);
 
	newTextBoxDiv.after().html('<label>Textbox #'+ counter + ' : </label>' +
	      '<input type="text" name="textbox' + counter + 
	      '" id="textbox' + counter + '" value="" >');
 
	newTextBoxDiv.appendTo("#TextBoxesGroup");
 
 
	counter++;
     });
 
     $("#removeButton").click(function () {
	if(counter==1){
          alert("No more textbox to remove");
          return false;
       }   
 
	counter--;
 
        $("#TextBoxDiv" + counter).remove();
 
     });
 
     $("#getButtonValue").click(function () {
 
	var msg = '';
	for(i=1; i<counter; i++){
   	  msg += "\n Textbox #" + i + " : " + $('#textbox' + i).val();
	}
    	  alert(msg);
     });
  });
</script>
</head><body>
 
<div id='TextBoxesGroup'>
	<div id="TextBoxDiv1">
		<label>Textbox #1 : </label><input type='textbox' id='textbox1' >
	</div>
</div>
<input type='button' value='Add Button' id='addButton'>
<input type='button' value='Remove Button' id='removeButton'>
<input type='button' value='Get TextBox Value' id='getButtonValue'>
 
</body>
</html>
Advertisements

April 11, 2013

How to get a radio button’s label text with jQuery

Filed under: Javascript / JQuery — Duy Nguyen @ 9:59 pm
Tags: , , , , , ,

From stackoverflow

<input type="radio" name="value" id="product1">
<label for="product1">Product One</label>

You can obtain the label’s text with this jQuery code:

var text = $("input:checked + label").text();

The + signifies the adjacent selector. It will only select a label which has a checked input next to it.

If your label element has to be placed before the input, a single CSS selector can’t get a reference to it. Using jquery, you can however get a reference to the “previous child element” with the prev() function.

var text = $("input:checked").prev().text();

September 5, 2011

How to write and append to a text file (C# .NET)

Filed under: C# — Duy Nguyen @ 2:33 pm
Tags: , , , , ,

How write to a text file and append more text from another function.


namespace TextFileWriter
{
    public partial class _Default : System.Web.UI.Page
    {
        //Create the StreamWriter object to do the writing
        StreamWriter sw;

        protected void Page_Load(object sender, EventArgs e)
        {
            string driveLetter = "c";
            string folder = "DEV_LOGS";
            string filename = "myFile";
            string fileExtension = ".txt";

            string path = SetFilePath(driveLetter, folder);
            string fullFilePathName = path + "\\" + filename + fileExtension;

            sw = CreateTextFile(fullFilePathName);

            sw.WriteLine("My first line of text");

            function1();

            sw.WriteLine("My third line of text");

            //be sure to close the stream and file or else
            //the file will be locked
            sw.Close();
        }

        public void function1()
        {
            sw.WriteLine("Second line of text");
        }

        public StreamWriter CreateTextFile(string fullFilePathName)
        {
            StreamWriter sw;

            // checks if the file exists
            if (!File.Exists(fullFilePathName))
            {
                sw = File.CreateText(fullFilePathName);
            }
            else
            {
                // file exists, append text to file
                sw = File.AppendText(fullFilePathName);
            }

            return sw;
        }

        public string SetFilePath(string driveLetter, string folder)
        {
            // Specify a "currently active folder" 
            string activeDir = driveLetter + @":\";

            //Create a new subfolder under the current active folder 
            string newPath = System.IO.Path.Combine(activeDir, folder);

            // Create the subfolder
            System.IO.Directory.CreateDirectory(newPath);

            return newPath;
        }
    }
}

Output:

My first line of text
Second line of text
My third line of text

You can also append like this:

namespace TextFileWriter
{
    public partial class _Default : System.Web.UI.Page
    {       
        //Create your objects here for global scope
        //Create the file
        FileStream theFile;

        //Create the StreamWriter object to do the writing
        StreamWriter sw;

        int x;

        protected void Page_Load(object sender, EventArgs e)
        {
            x = 0;

            for (int i = 0; i < 5; i++)
            {
                function1();
            }
        }

        public void function1()
        {
            x++;

            theFile = new FileStream(@"C:\DEV_LOGS\test.txt", FileMode.Append);

            sw = new StreamWriter(theFile);
            sw.WriteLine(x.ToString() + " line of text");

            sw.Close();
            theFile.Close();
        }
    }
}

Output:
1 line of text
2 line of text
3 line of text
4 line of text
5 line of text

Check this site out for different ways to open a file.

How to write to a text file with classic ASP

Filed under: Classic ASP — Duy Nguyen @ 1:20 pm
Tags: , , , ,

Here is a quick snippet on how to write to a text file with with Classic ASP. (VBscript)

dim filesys, filetxt
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set filesys = CreateObject("Scripting.FileSystemObject")
Set filetxt = filesys.OpenTextFile("c:\somefile.txt", ForAppending, True)
filetxt.WriteLine("Your text goes here.")
filetxt.Close 

Create a free website or blog at WordPress.com.