How to Change textbox text into another textbox in asp.net C#? - javascript

I am having two text boxes on my web page as txtGivenName and txtDisplayName. Now I want to display txtGivenName text on txtDisplayName while entering text on txtGivenName by using keypress event or keydown event.
I have tried some code for achieve this but not able to get the textbox1 value on textbox2.
My Code is:
<asp:TextBox ID="txtGivenName" runat="server" onkeypress = "copyText()" ></asp:TextBox>
Script code is:
function copyText() {
var givenName = document.getElementById("txtGivenName");
var displayName = document.getElementById("txtDisplayName");
displayName = givenName;
}
but nothing happened while entering text on txtGivenName. Can anyone suggest me to achieve this or tell me any other way to achieve this?
(Note: I dont want to do this by OnTextChanged event, Bcoz it will populate the first text box value on second one after text enter got over only. While entering the text on first textbox we need to change the text on second textbox simultanously)
Thanks.
I got the exact answer for this feature by using below mentioned script code.
Solution:
function OneTextToOther() {
var first = document.getElementById('<%= txtGivenName.ClientID %>').value;
document.getElementById('<%= txtDisplayName.ClientID %>').value = first;
}

Take a look at the page source in the browser -- "txtGivenName" and "txtDisplayName" are not going to be the IDs of the text boxes because ASP.NET prepends the IDs based on the control hierarchy so they are globally unique.
You have two options -- use "<%=txtGivenName.ClientID%>" to get the true name of the text box in javascript, or set ClientIdMode="static" on the text boxes so the IDs are left alone.
function copyText() {
var givenName = document.getElementById("<%=txtGivenName.ClientID%>");
var displayName = document.getElementById("<%=txtDisplayName.ClientID%>");
displayName.value = givenName.value;
}

You can try keyup event but I know only Jquery method to do it.
<input class="one" type="text"/>
<input class="two" type="text"/>
$(".one").on("keyup", function(){
$(".two").val($(".one").val());
});
here is the demo: http://jsfiddle.net/pUpZA/
PS: the asp:textbox translates to input when you compile so its basically the same thing. use "CssClass=one" for your asp:textboxes

Related

Get selected text in an input box

Is it possible to get the selected text in an input box of a website, using either jQuery or vanilla JavaScript?
I have tried with var selectedText = window.getSelection().toString();,
but this code only gets the text in a paragraph and not in an input box.
EDIT: Maybe I was unclear, I want to get the text from a website that I didn't create. I'm building a Chrome extension and I need to get the text from an input box of a website.
Here is a solution:
function showSelectedText() {
var input = document.getElementById('text');
var selection = text.value.substring(input.selectionStart, input.selectionEnd);
alert(selection);
}
<input id="text" value="Hello, how are you?"><br>
<button onclick="showSelectedText()">Show selected text</button>
If you don't mind using jQuery plugins you can accomplish that by using this one http://madapaja.github.io/jquery.selection/
It's flexible (You can use it both for inputs and for paragraphs)

Get changed text on a input field using jQuery

So, I know how to detect when a user has changed the text on a input field (including typing, pasting, and other means)...
But for my purpose, I need to know exactly what was changed in the text field.
For example, initially, the text field contains this:
This is a text
Then the user changes it to this:
This is my text
The script should return this to me: text position from 8 to 9 was changed to my.
So is there an easier way to do this without having to use a difference checker algorithm? Because it seems that if I put an event for keyup, I can detect what position was changed or deleted, but this won't detect the whole text pasted from Ctr+V. Anyways, I need a fast way to send the differences in a text to synchronize it with the database on the server using AJAX, kinda like Google Docs, but I feel like resending the whole text field every so on and then is very inefficient, mainly because the user may be editing a very large text field.
Try this:
<script>
$(document).ready(function(){
var inival=$("#myinput").val();
$("#check").click(function(){
$("#log").empty();
var newval=$.trim($("#myinput").val());
var splitval=newval.split(' ');
for(var i=0; i<splitval.length; i++){
if(inival.indexOf(splitval[i])<0){
var charindex=newval.indexOf(splitval[i]);
$("#log").append('text position from '+charindex+' to '+(charindex+1)+'
was changed to '+splitval[i]+'<br>');
}
}
});
});
</script>
<input type="text" value="This is a text" id="myinput"><input type="button"
value="Check" id="check"><br>
<div id="log"></div>
jsfiddle

Clear Asp.Net TextBox Control on OnFocus with Jquery or Javascript

I have been scouring the interwebs and stackoverflow, but I have not come across a solution that works. I need to erase the default text from the textbox when onfocus happens. I know it is hitting onfocus because my class for text color is set in there also and the color is changing. Here's my code and what I have tried.
ASP:
<asp:TextBox ID="txtAddEditDel" runat="server" CssClass="txtAddEditDel" ></asp:TextBox>
The default text is some random instructions it populates on Page_Load.
Jquery:
$(".txtAddEditDel").focus(function () {
var selectedRoleText = $(this).find('option:selected').text();
var selectedRoleId = $(this).find('option:selected').val();
var selectedRoleIndex = $(this).find('option:selected').index();
//enable add button
enableDisableLinkButtons(true, true, true);
if (selectedRoleText == 'Create, Edit, or Delete a Role Name.') {
$(".txtAddEditDel:text").val('');
}
$(".txtAddEditDel").removeClass('placeholderText');
});
The removeClass function is hit, but my value is not cleared.
Heres what I have tried:
$(".txtAddEditDel").val('');
$(".txtAddEditDel").val("");
$(".txtAddEditDel").text("");
$(".txtAddEditDel").text == '';
$(".txtAddEditDel").text == "";
$(this).val('');
document.getElementbyId('<%= txtAddEditDel.ClientID %'>.value = "";
$(".txtAddEditDel").val(''); should work, assuming its getting there. Have you put some debugging information to check the if clause is true? It may be a better idea to check against the value of the option rather than the text, as the text could easily be misspelt or changed where as a default value will, in my experience, be '' or 0 in this scenario

How can I set the text of this ASPX control using JavaScript?

I have the following input control in an ASPX page:
<input tabindex="0" class="_f_ql _f_rl textbox allowTextSelection placeholderText" role="textbox" aria-labelledby="MailCompose.SubjectWellLabel" autoid="_f_B2"></input>
I am only able to inject JavaScript into this page, and check if an input element with the above class is present, and I would like to change the value of the text that is inside of this textbox. Is there any way to do this with JavaScript?
So far, I can get the correct element using the following code, but setting the value on it does not work to set the textbox's text to Testing...:
var subjectElements = getNodeElementsContainingID(document.body, "_f_ql _f_rl textbox allowTextSelection placeholderText");
if (subjectElements.length > 0) {
subjectElements[0].value = "Testing...";
}
I don't want to inject jQuery as its overkill, but seeing as how I have the element I need, what can I do to it to set the text value on it?
Try
document.getElementsByClassName('_f_ql _f_rl')[0].value = 'Testing...';

How to update 2 textboxes with javascript?

How can I update a multiple textboxes value with Javascript, when user change the value of textboxes,
and I have a value already on those textboxes?
Am I doing in the right track on the code below?
<input type="text" id="amount_textbox" onChange="UpdateValue()" value="100">
<input type="text" id="amount_textbox" onChange="UpdateValue()" value="200">
function UpdateValue()
{
//alert ("you have changed the textbox...");
var x = document.getElementById('amount_textbox').value;
document.getElementById("amount_textbox").setAttribute("value", x);
document.getElementById('amount_textbox').value = x;
}
That function is working only for the first textbox, the second one can not update, how can I make other textbox work?
in jquery
$("#text").val("my new value");
in javascript
document.getElementById("text").setAttribute("value", "my new value");
document.getElementById('text').value = 'Blahblah';
First of all, I'm not sure why you'd want to set the value of the textarea(?) to itself - doesn't quite make sense, but here's the code nevertheless.
function checkTotal()
{
var tbox = document.getElementById('ammount_textbox');
var val = tbox.innerHTML;
tbox.innerHTML = val;
}
Your event handler is onClick, but it seems like you want to prevent changes? A click is not a change to the content of a form text box. Perhaps you want onChange?
change your onClick to onKeyup. With textareas, I have been able to access the value using just the value. Since the content is between opening and closing tags, you might be able to use the javascript innerHTML property ("That works with the button tag instead of value"). Good Luck!
function UpdateValue()
{
alert ("you have changed the textbox...");
var x = document.getElementById('amount_textbox').value;
document.getElementById("amount_textbox").setAttribute("value", x);
document.getElementById('amount_textbox').value = x;
}

Categories

Resources