3

I am working in ASP.NET project. And my task is to append the Checkbox text to the TextBox. Checkboxes's text are bound from Database values in CheckBoxList.

protected void prbtn_Click(object sender, EventArgs e)
{
    string ConnectionString = "Data Source=.;Initial Catalog=nci;Integrated Security=true";
    SqlConnection myConn = new SqlConnection(ConnectionString);
    List<string> minire = new List<string>();
    string sql = "SELECT distinct PRIMARY_MINI_REGION FROM customers";
    myConn.Open();
    SqlCommand cmd = new SqlCommand(sql, myConn);
    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    adapter.Fill(dt);
    minire.Add(dt.Rows[0][0].ToString());
    group12.DataSource = dt;
    group12.DataTextField = "PRIMARY_MINI_REGION";
    group12.DataValueField = "PRIMARY_MINI_REGION";
    group12.DataBind();
    string[] grpary = prgrp.Text.Split(';');

    foreach (var items in grpary)
    {
        if (items != "")
        {
            li.Add(items.ToString());
            if (li.Contains(items.ToString()))
            {
                group12.Items.FindByText(items.ToString()).Selected = true;
                //group12.Items.FindByText(items.ToString()).Enabled = false;
            }
        }
    }
}

Can any of you please help me how to do it in jquery.Currently i appended text using stringbuilder using C# as like

protected void group_SelectedIndexChanged(object sender, EventArgs e)
{
    foreach (ListItem l in group12.Items)
    {
        if (l.Selected)
        {
            li.Add(l.Text);
        }
    }
    foreach (var i in li)
    {
        si.Append(i.ToString() + ";");
    }
    //if (prgrp.Text == "")
    //    prgrp.Text = si.ToString();
    //else
    prgrp.Text = si.ToString();
}

once i select checkbox means it have to append text.and once i unchecked it means,content have to be deleted from textbox

2 Answers 2

1

I guess this snippet helps you out:

$(function() {
	$('input[type="checkbox"]').change(function() {
		// Reset output:
		$("#output").html('');
		
		// Repeat for all checked checkboxes:
		$('input[type="checkbox"]:checked').each(function(){ 
			
			// Get values:
			var existingText = $("#output").html();
			var textToAppend = $(this).val();
			
			// Append seperator (';') if neccessary:
			if(existingText != '')
			{
				existingText = existingText + ";";
			}
			
			// Print out append value:
			$("#output").html(existingText + textToAppend);
		});
	});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<h2>Select:</h2>
<input type="checkbox" value="Jan"/>Jan
<input type="checkbox" value="Feb"/>Feb
<input type="checkbox" value="Mar"/>Mar
<input type="checkbox" value="Apr"/>Apr

<h2>Output:</h2>
<div id="output"></div>

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, I misunderstood. I just corrected the code snippet.
Help me with this link please....stackoverflow.com/questions/28832368/…
Thanks 5T..can u help me in reverse process too..if i typed that check box value in textbox means..it have to check the appropriate checkbox
1
 $(document).ready(
    $('.checkBoxCommunClass').on('click', function () {
        var id = $(this).get(0).id;
        if ($('input[id=' + id + ']:checked').length > 0)
            $('#resultTextInput').val() = $('#resultTextInput').val() + $(this).val();
    }));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.