How to hide blinking cursor in Angular 2? - html

I was working on search bar where user input the keyword. When the user hit the enter key, results are displayed. I want to implement in this way -
Whenever, user hits enter to search results for a keyword, you can see there is no blinking cursor at the end of keyword. This don't happen when you go to google.com and click on input box. At that time, we see a blinking cursor.
The problem I'm facing is that, when a user hits enter key, results are displayed but cursor remains blinking in search bar. What should I do to solve this problem so that clicking on input box should display a blinking cursor but on hitting enter key, it hides blinking cursor ?
NOTE - Since I don't know, how this problem is being generated I haven't provided the code. I just wanted to have some rough idea with code how should I proceed. :)

This is done by taking the focus away from the input field, which requires a tiny bit of javascript.
I don't know how your code works as you aren't providing the code but form submit would be I imagine, how you would trigger the action on enter?
So I'll do a quick example...
We have a form which is populated with an input field.
var form = document.querySelector('#form');
var input = document.querySelector('#text');
form.onsubmit = function(e) {
e.preventDefault();
text.blur();
}
<form id="form">
<input type="text" id="text">
</form>
Using .blur takes the focus away from the input.
I miss read your question and saw that you were doing this with Angular, however I will leave the original answer there for anyone else that may stumble upon this answer.
To do it in Angular, it has a lovely attribute of ng-enter.
We can then add this to the input by doing <input type="text" ng-enter="$scope.blur()" />
Then we can create this action by doing
$scope.blur = function($event) {
var input = $event.target;
input.blur();
}

Related

How to fix Angular bug requiring user to click a separate element before choosing a second mat chip

Here is the link for an example of the issue I will attempt to describe. In the chips autocomplete example, click the text box to select a new fruit.
Now, before clicking anywhere else, click again on the text box as you did before.
This should result in no options showing up. The issue here is that the user must either begin keying in a new selection or first click another element in the window before matchip will show the options to choose from. I am wondering if there is a way to fix this issue. I would like a user to be able to choose a selection from the list and then immediately click the text box as they had before and make a new selection.
I'm using mat-chip-list inside an outer *ngFor iterating over a FormArray.
Here is what I'have done. It's pretty efficient :
<input
#validatorInput
#operationTrigger="matAutocompleteTrigger"
[formControl]="contactCtrl"
[matAutocomplete]="auto"
[matChipInputFor]="chipList"
(blur)="contactCtrl.setValue(''); validatorInput.value='';"
(click)="contactCtrl.setValue(''); validatorInput.value=''; operationTrigger.openPanel()">
The trick is
Always clear your html input and your (shared) formControl with an empty and not null value each time the blur and click events occur.
Do NOT do this 'clear' on the input focus event. (Because when you delete the last chip, the input is auto-focus and you will have the famous Expression has changed after it was checked.
Call operationTrigger.openPanel(); when the user click on the input
Setting contactCtrl.setValue(''); allows your autocomplete panel to be automatically opened when you call operationTrigger.openPanel()
Setting validatorInput.value=''; is just a way to properly sync your formControl with the html input to ensure a good UX/UI behavior.
Inside my formArray, the formControl is the same for all the inputs but it does not matter since the user can only manipulate one input at a given time
Since you didn't post your code and you mention the example on the material site I'm going to do it as a fork of the stackblitz example they have on their site.
But this will allow you to open the autocomplete panel again despite having had the cursor there and choosing an option previously.
// Using MatAutocompleteTrigger will give you access to the API that will allow you to
// to open the panel or keep it open
...
#ViewChild(MatAutocompleteTrigger, {static: false}) trigger: MatAutocompleteTrigger;
...
ngAfterViewInit() {
fromEvent(this.fruitInput.nativeElement, 'click')
.pipe(
tap(() => {
this.trigger.openPanel()
})
).subscribe()
}
Link to the full stackblitz:
https://stackblitz.com/edit/angular-sb38ig

Cannot clear text in input text field in Ionic

I'm new at Ionic and Angular. I try to create a post status like Facebook, my first task is when the user types any text and press the POST button I want the text in the input field to disappear, but it disappears in the first time when this view is a new load.
When I try to type any text in the input field and press the POST button, the text won't disappear but function in the controller was called. Past, I use label tag but when I press the POST button ng-click didn't work so I change label tag to div tag but it work at the first time as I say above and I include cache is false it didn't work too. Here is my Plunker, This issue is in Chat view.
The ng-model should be bound to an object and not to an variable.
The means the the declaration of the variable should be something like:
$scope.input = {
saySomething : 'Some question'
};
The input field should then be:
<input type="text" placeholder="Say Something" ng-model="input.saySomething"/>

why do I have to click twice to a submit input using selenium

ENV
chrome 32 webdriver2.8
I am using selenium(java) to click a submit input. But I need to click twice to active the submit operation.
The input code:
<input type="submit" disabled="disabled" id="id_submit" name="submit" class="btn-txt" value="OK">
NOT WORK selenium code:
if(submitButton.isEnabled()) {
new Actions(driver).moveToElement(submitButton).perform();
submitButton.click(); // this sentence is executed.
}
WORKS
submitButton.click();
submitButton.click();
Well, the first click function seems to make the button get focus, and the second click function active submit operation. When I use moveToElement to focus the button first, the click not work either. I can ensure that the button is enabled. I want to use selenium to click button and I dont want to do click twice. What should I do? Any ideas? Thanks.
EDIT
WORKS EITHER
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById('id_submit').focus();");
jse.executeScript("document.getElementById('id_submit').click();");
UPDATE
Imagine that I want to input a string, say "abcdef123456". Then send_keys is executed. It looks like
and submit button is enabled. After click the submit button at the first time. It looks like and submit button get focus. After click the submit button at the second time, the form is submited and page is redirected. I am confusing about the first click.
have you tried with Expected Conditions class,
new WebDriverWait(driver,30).until(ExpectedConditions.elementToBeClickable(By.id("id_submit"))).click();
It can solved like:
driver.sendkeys("abc1234"); (input in text box)
//explicit wait
WebDriverWait w = new WebDriverWait(driver, 10);
w.until(ExpectedConditions.elementToBeClickable(By.id("Button")));
or
w.until(ExpectedConditions.visibilityOf((WebElement) By.id("Button")));
driver.findElement(By.id("Button")).click();

Setting an HTML text input box's "default" value. Revert the value when clicking ESC

When a web form is written to the browser, the browsers remembers what the initial values are of a text INPUT box. ie. when it receives HTML like this:
<input type="text" value="something">
The browser remembers "something" as the initial/default value. When the user starts typing over it, then hits ESC, the browser reverts the field to the initial value (or blank if it was initially blank of course).
However, when creating a text input box programatically, hitting ESC always seems to blank the box, even if I create it with a default value like so:
$('<input type="text" value="something">')
The browser doesn't count this as a default value and doesn't revert to it when hitting ESC. So my question is, is there a way to create a text box in code and somehow assign it a default value, so the ESC key works as if the browser received it in the HTML document?
You might looking for the placeholder attribute which will display a grey text in the input field while empty.
From Mozilla Developer Network:
A hint to the user of what can be entered in the control . The
placeholder text must not contain carriage returns or line-feeds. This
attribute applies when the value of the type attribute is text,
search, tel, url or email; otherwise it is ignored.
However as it's a fairly 'new' tag (from the HTML5 specification afaik) you might want to to browser testing to make sure your target audience is fine with this solution.
(If not tell tell them to upgrade browser 'cause this tag works like a charm ;o) )
And finally a mini-fiddle to see it directly in action: http://jsfiddle.net/LnU9t/
Edit: Here is a plain jQuery solution which will also clear the input field if an escape keystroke is detected: http://jsfiddle.net/3GLwE/
This esc behavior is IE only by the way. Instead of using jQuery use good old javascript for creating the element and it works.
var element = document.createElement('input');
element.type = 'text';
element.value = 100;
document.getElementsByTagName('body')[0].appendChild(element);
http://jsfiddle.net/gGrf9/
If you want to extend this functionality to other browsers then I would use jQuery's data object to store the default. Then set it when user presses escape.
//store default value for all elements on page. set new default on blur
$('input').each( function() {
$(this).data('default', $(this).val());
$(this).blur( function() { $(this).data('default', $(this).val()); });
});
$('input').keyup( function(e) {
if (e.keyCode == 27) { $(this).val($(this).data('default')); }
});
If the question is: "Is it possible to add value on ESC" than the answer is yes. You can do something like that. For example with use of jQuery it would look like below.
HTML
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<input type="text" value="default!" id="myInput" />
JavaScript
$(document).ready(function (){
$('#myInput').keyup(function(event) {
// 27 is key code of ESC
if (event.keyCode == 27) {
$('#myInput').val('default!');
// Loose focus on input field
$('#myInput').blur();
}
});
});
Working source can be found here: http://jsfiddle.net/S3N5H/1/
Please let me know if you meant something different, I can adjust the code later.
See the defaultValue property of a text input, it's also used when you reset the form by clicking an <input type="reset"/> button (http://www.w3schools.com/jsref/prop_text_defaultvalue.asp )
btw, defaultValue and placeholder text are different concepts, you need to see which one better fits your needs

Keeping the focus on an html form in spite of tab key

I am trying to create a webform in HTML, and if needed javascript. In this webform one should be able to enter source code, so to do that comfortably I would like one to be able to enter tabs.
Is there a way to achieve this?
Thanks
You might be able to capture the onKeyDown event. If the keycode is equal to the tab key, then replace the tab with 3 spaces or something like that.
UPDATE:
I tested this in firefox 3. Allows you to type a tab without loosing focus. Just be careful b/c this code will just append a tab character to the end of the text box. Thus, if the user types a tab in the middle of text, tab will still appear at the end.
<html>
<head>
<script>
function kH(e)
{
//capture key events
var pK = document.all? window.event.keyCode:e.which;
//if target is textbox and key is tab
if(e.target.type=='text' && pK==0)
{
//append tab to end of target value
e.target.value = e.target.value + "\t";
//Cancel key event
return false;
}
}
document.onkeypress = kH;
if (document.layers) document.captureEvents(Event.KEYPRESS);
</script>
</head>
<form>
<input type='text' id='txtTest' name='txtTest'></input>
</form>
</html>
There isn't a good way...that's why stackoverflow makes you do 4 spaces and uses a special library to interpret 4-space indented stuff as code. I suppose if you really wanted to use tabs you could do an onBlur event handler which just gave focus back to the window, and an onKeyDown event handler that inserted 4 spaces whenever the TAB key was pressed.
You could go for an "indent" button on the toolbar. When it is pressed, it either inserts a tab if nothing is selected, or indents the selection.