focus() doesn't work in input (all browsers) - javascript

I know there is tons of questions like that on stackoverflow but none of them solved my problem. I'm trying to build a small "pyramid" word game. You have to find the right translation for a korean word, then a new input with a new word will pop up. Solve this word, the next input field will pop up, ...
HTML:
<input id="ha_1" class="halter" placeholder="안녕하세요" type="text" onblur="checkSol(id);"></input><br/>
<input id="ha_2" class="halter" placeholder="얼굴" type="text" onblur="checkSol(id);"></input><br/><br/>
<input id="ha_3" class="halter" placeholder="문" type="text" onblur="checkSol(id);"></input><br/><br/>
Here is the relevant piece of code:
// compare if user input is correct solution
if (user_input == solution){
// if correct, display the next input field:
$('#' + e_dies).nextAll('.halter:first').css('display', 'block');
// ==> supposed to focus the fadedIn input
$('#next_input').focus();
// count up to the next word
l_count++;
// give correct/incorrect feedback
if(l_count == last_sol){
tell_me.innerHTML = 'Correct';
return false;
};
} else if(document.getElementById(e_dies).value == "") {
tell_me.innerHTML = '';
}
This line here $('#' + e_dies).nextAll('.halter:first').css('display', 'block'); displays a input field. Right after, $('#next_input').focus(); is supposed to focus this input field , which just faded in. I tried some solutions like using setTimeout or moving it to the end of the function. Nothing worked for me.
Strangely, other commands like $('#next_input').css('color', 'red'); work just fine, just the .focus() makes trouble.
Help would be much appreciated!

Though the question seems to be not that clear i have implemented a small fiddle showing input box focus working on conditional value.I hope this can give some help
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
function checkSol(id){
var solution="testname";
console.log(event);
var user_input=event.target.value;
if (user_input == solution){
// if correct, display the next input field:
// ==> supposed to focus the fadedIn input
$('#focusinputbox').focus();
}
}
</script>
<input id="ha_1" class="halter" placeholder="안녕하세요" type="text" onblur="checkSol(id);" /><br/>
<input id="ha_2" class="halter" placeholder="얼굴" type="text" onblur="checkSol(id);" /><br/><br/>
<input id="ha_3" class="halter" placeholder="문" type="text" onblur="checkSol(id);" /><br/><br/>
<input id ='focusinputbox' placeholder='inputtobefocussed'/>

Related

How do you make a checkbox that is non interactive and is only checked if variable is?

I need a script where, if a value is higher then an assigned value. A non-interactive checkbox checks itself to show the user that the value they inserted is higher.
You should come with some code or the example that you have tried, still following example can you help you with your problem:
HTML
JSFiddle: http://jsfiddle.net/par409yx
document.getElementById('boxText').onchange = function() {
if (document.getElementById('boxText').value > 10)
document.getElementById('box').checked = true
else
document.getElementById('box').checked = false
};
<input id="box" type="checkbox" disabled/>
<input id="boxText" type="text" />

Javascript enabling input field after disable=true

html:
<label>Label1</label><br>
<input type="text" name="first" onclick="somefunc()"><br>
<label>Label2</label><br>
<input type="text" name="second"><br>
Javascript:
function somefunc() {
var second = document.getElementsByName('second')[0];
second.disable = true;
}
When I click the first input the second is disabled (that was what I want), but when I type something into the first input field, then delete it, the second is still disabled. Is there a way so I can enable it again?
I couldn't find an other event which can solve this.
You can listen to the keyup event on the first input box and check the value of first input box for enabling or disabling second input.
<label>Label1</label><br>
<input type="text" name="first" onkeyup="somefunc()"><br>
<label>Label2</label><br>
<input type="text" name="second"><br>
<script>
function somefunc() {
var first = document.getElementsByName('first')[0];
var second = document.getElementsByName('second')[0];
if(first.value){
second.disabled = true;
}else{
second.disabled = false;
}
}
</script>
Seems you have missed enabling textbox here. If you can see in previous reply, you just need to re-enable textbox into same state as it was before.

delete function on multiple inputs

I have a js funciont that erase the last digit on an input, it work fine, but the problem is that i have another input and doesn't work. It just erase te digit on the first input.
<script>
function deleteTag(){
var strng=document.getElementById('entrada_1').value;
document.getElementById('entrada_1').value=strng.substring(0,strng.length-1);
}
</script>
<form method="POST" action="dashboard.php">
<label>RUT</label>
<input id="entrada_1" placeholder="12345678-9" type="text" name="rut">
<label>pass</label>
<input id="entrada_2" placeholder="pass" type="password" name="pass">
</form>
it works fine when is used on the input "entrata_1" but on "entrada_2" doesn't work, how can i make it work where the focus is?
You should instead just use a button without wrapping it in an anchor tag, give an onclick attribute like such onclick="deleteTag();". Give both the input fields a class name like class='entrada'.
Then in the function:
function deleteTag(){
var allInputs = document.querySelectorAll('.entrada');
allInputs.forEach((input) => input.value = input.value.substring(0, input.value.length - 1));
}

How to set text box to appear blank after .click function

So I have a simple log in that requires a user to input values from a json file into two different text boxes ,when the user name and (in this case I have used ID as password) matches then an alert appears to say... "welcome"
After the .click function is carried out the users text still remains in the text box, how can I get both text boxes to appear blank after the .click function?
$(document).ready(function() {
//Hide alert when page loads
$("#loginalert").hide();
$("#invalid").hide();
$("#loginbtn").click(function(event){
$.getJSON('result.json', function(jd) {
var id = $('#userName').val();
var name = $('#userName2').val();
var valid = false;
for (var i=0; i<jd.user.length; i++) {
if ((jd.user[i].ID == id) && (jd.user[i].name == name)) {
valid=true;
$('#loginalert').html('<img src="' + jd.user[i].imgpath + '"><br><p> Welcome: ' + jd.user[i].name + '</p><button type="button" id="btnhide" class="btn btn-primary btn-md">Hide</button>');
//show the alert after loading the information
$("#loginalert").stop().fadeIn('slow').animate({ opacity: 1.0 }, 3000)
$('#invalid').hide();
$('#btnhide').on('click', function(e){
//console.log('here');
e.preventDefault();
$('#loginalert').hide();
});
}
}
if (!valid) {
$('#invalid').fadeIn('slow');
$('#loginalert').hide();
}
});
}); });
username 1 and #username 2 are the text boxes - is there any way to get user name 2 to display in stars ****** when the user enters the password - this question is not that necessary but if i could also get that working that would be good.
thanks guys hope someone can help :)
is there any way to get user name 2 to display in stars ****** when
the user enters the password
You can use an input box with text property set as password. But that password masking character will be . instead of *. Not exactly sure, whether it will be a different character in some browsers.
<input type="password" id="txtPassword" />
text box to appear blank after .click function
You can set the .val() property of the jQuery objects of two those two textboxes.
$('#userName, #username2').val('');
Use <input type="password"> to show typing as stars.
Clear inputs by setting their value to be empty: $('#userName').val('');
And perhaps consider breaking your code down into a couple smaller functions so it's easier to follow.
document.getElementById("#myTextbox").value="";
This should get your textbox and set the value of it to "", which is blank.
Edit: JSFiddle
Another Method:
You can also add the script directly inside the button without using/creating a function.
<input id="inputId" type="name" />
<button onclick="document.querySelector('#inputId').value='';"> Clear </button>
Using querySelector:
<input id="inputId" type="name" />
<button onclick="click()"> Clear </button>
<script>
function click() {
document.querySelector('#inputId').value="";
}
</script>

How to instantly review text inside input

I've seen some sites that using jquery to review instantly any text wrote inside an input text field.
Here is example :-
where i write a it instantly shown at => some_site.com/a
when i write another letter b it instantly shown at => some_site.com/ab
and so on anything i wrote instantly shown
But that is not all ! if i removed any text so the input field is empty
it shows => some_site.com/???
This could be good for reviewing input text before submit the whole form
How to do such nice effect ?
if html form code is
<input type="text" name="txt" id="txt">
~ thanks for help
Here is a working example
http://jsfiddle.net/ydzr8/
basically you want the keyup event to get the value from the text box
<input type='text' class='input'/> <div class="display">http://www.something/???</div>
Then
$('.input').keyup(function(){
if($(this).val() === '')
{
$('.display').html("http://www.something/???");
}else{
$('.display').html("http://www.something/" + $(this).val());
}
});
Since you tagged Mootools in this question, here is a Mootools way to do it:
HTML example:
<input type='text' class='input' />
<div id="result">http://www.mysite.com/<span></span>
</div>
Mootools:
document.getElement('input').addEvent('keyup', function () {
var val = this.value ? this.value : '???';
document.id('result').getElement('span').innerHTML = val;
});
// Option 2:
document.getElement('input').addEvent('keyup', function () {
$$('#result span').set('html', this.value ? this.value : '???');
});
Demo here

Categories