I'm trying to give the user ability to create customized list for my application,by insert his choices in text area field ,each line is treated as one choice using the \n (enter) as separator , i need to allow user to enter one choice in multiple lines,any one can help?
<textarea></textarea>
user enter :
aa
bb
cc
with enter in textarea, and then I'm processing them to split them at enter separator ,so i have three option in list :1-aa ,2-bb,and 3-cc ,but what i need to make aa ,bb is the first option, and cc second option ,i need new separator, i need more ideas?
I think you're saying that you need the user to enter listable items in a textarea and need to support the ability to present a single option across multiple lines.
As you've seen, using a single newline as the item separator does not work - you can't differentiate between two items on one line each and a single item presented on two lines.
Use two newlines instead of one as the item separator.
Example input:
Item One
Item Two line 1
Item Two line 2
Item Three
You can use whatever separator you like. For example an empty line (as when separating chapters), or a comma or semicolon. Any character (or sequence of characters) that is not part of the text to be entered.
Just remember to tell the user what to use as a separator.
This code will allow for the options to be entered on multiple lines and the options are created once you press Enter twice:
JavaScript
<script type="text/javascript">
var LastKeyCode=-1;
function updateOptions(e) {
if (e.keyCode) {
var KeyCode=e.keyCode;
} else {
var KeyCode=e.charCode;
}
if (KeyCode==13 && LastKeyCode==13) { // Enter?
var RowsDataAry=document.getElementById('inputfield').value.replace(/\r\n/g,"\n").split(/\n{2,}/g); // Fix IE CRs and split the textarea input
document.getElementById('choices').options.length=0; // Empty select
for (RowID in RowsDataAry) {
var TextVal=RowsDataAry[RowID].replace(/\n/g,", ").replace(/, $/,""); // Set commas and remove the last comma
document.getElementById('choices').options.length++;
document.getElementById('choices').options[RowID].value=RowID; // Add option value
document.getElementById('choices').options[RowID].text=TextVal;
; // Add option text
}
}
LastKeyCode=KeyCode;
}
</script>
HTML
<select id="choices" size="20"></select>
<textarea id="inputfield" onkeypress="updateOptions(event)" cols="40" rows="20"></textarea>
Tested in Firefox 3.6.3, Opera 10.53, IE 8 and Iron 5.0.380 .
Related
I am trying to select an option to be viewed from a select dropdown. The code works by selecting the first characters of the element (e.g "Wood_20Sept" "Fire_20Sept" "Water_20Sept"). Snippet of the working code:
filterList(document.getElementById("worldElementsList"), /^Wood/i);
So this would select any options with the word "wood".
Like:
"Wood_20Sept"
"Wood_20Mar"
"Wood_20May"
Problem is now there is a version 2 of these groups ("Wood_20Sept_V1" "Wood_20Sept_V2" "Fire_20SeptV1" "Fire_20SeptV2")
So I want to select the options by first and last: "Wood" and "V2".
So basically I need the code to say this:
filterList(document.getElementById("bankInfoList"), /^Wood/i && /v2$/i);
But that will only select the "V2"
Follow below steps:
A.Take whole string as a array
B. Find array[length-1] and array[length-2]
To match last elements with your string
Is there any way I can remove a match of a set of first 3 characters in an input field, once a user is done typing, with jQuery?
If you want to remove first three characters after user has moved his cursor to other field, use this code...
$('#yourInputField').blur(function(event) {
field = event.target;
value = field.value;
$('#yourInputField').val(value.substring(3,value.length));
});
I have a textbox ,i want whenever user start entering a number in that texbox the previous value should be remove.
Let say there is a textbox which contain a number as 23 ,i want whenever user start typing in that same textbox 23 shold be remove and new enetered number should be shown.
You can use the select function on text inputs and textareas to select all of the text. Then when the user types, the text gets replaced.
For example, if the input or textarea has the id "target":
document.getElementById("target").select();
Live Example | Source
Maybe you can do something like this
<input type="text" onclick="clear(this);">
And then make the javascript function named clear (or whatever name you want)
<javascript type="text/javascript">
function clear(element)
{
element.value = "";
}
</script>
I am trying to create a text field for entering dates that accepts only digits. If any one enter any other character, no need to display in the text field and the cursor need to remain in the same position(no need to move to the next character position). If the entered value is a number , then need to show in the text field and need to move the cursor to the next level.
So at last the text field contains only the numbers.
I am using the following code,
$("#date").keyup(function(event){
//var c=(event).keyCode;
var c= String.fromCharCode(event.keyCode);
var cval = $(this).val();
alert("Characters="+c);
if(isNumber(c))
$(this).val(cval+c);
}
Advanced Thanks,
VSoft
In HTML5 you can use input type as number.
You can do this:
$('#date').keyup(function () {
this.value = this.value.replace(/[^0-9\.]/g,'');
});
I think you need jQuery Input Mask Plugin
you should run your function only when Focus is on the requested text field
I've a text field.
I'm to type the email in it.. eg. username#domain.com
What I want is that, as soon as the user types the char "#", the remaining part of the string gets masked so that it appears to the user viewing as username#**********
But in the backend, the real keys typed has to be captured somehow.
Is there a way to do that using javascript?
I have no idea why you'd want to do that, but you could add an onKeyPress function to the text field which adds the typed letter to a hidden field and then updates the text field with the starred-out copy.
Try with jQuery
$("input").keyup(function(){
var charIndex = $(this).val().indexOf("#");
if(charIndex!=-1){
charIndex++; // to start with character after #
$("#hidEmail").val($(this).val());
var replaceStr = "";
for(i=0;i< ($(this).val().length-charIndex) ; i++){
replaceStr=replaceStr+"*";
}
$(this).val($(this).val().replace($(this).val().substr(charIndex),replaceStr));
}
});
You can use OnKeyDown event to capture the user input.
Using a hidden field, keep the real value in there. When a new character is pressed, add it to the value from the hidden field and in the text field display the masked text.
Also you have to be carefull with special characters (backspace in particular).