Using focus() in JavaScript - javascript

I am trying to use JavaScript to Make Focus on Text Box.But It does't works,Please tell the way to Using focus() and set null value for that text Box.
Script:
<script type="text/javascript">
$(document).ready(function ()
{
var str = ('#ViewData["ControlView"]'); //alert(str);
if (str == "1")
ShowProduct();
else
ShowGrid();
});
var message = ('#ViewData["Success"]');
if (message == "Product Code Already Exits.")
{
document.getElementById("Item_Code").value ="";
document.getElementById("Item_Code").focus();
}
View:
#Html.TextBox("Item_Code", "", new { #Maxlength = "10", id = "Item_Code" });

The code for focus() in Javascript is:
var message="message";
if(message=="")
{
document.getElementById("Item_Code").focus();
document.getElementById("Item_Code").value=="";
}

Jquery solution
<script type="text/javascript">
$(document).ready(function ()
{
var str = ('#ViewData["ControlView"]'); //alert(str);
if (str == "1")
ShowProduct();
else
ShowGrid();
});
var message = ('#ViewData["Success"]');
if (message == "Product Code Already Exits.")
{
$("#Item_Code").val("");
$("#Item_Code").focus();
}

Try using jQuery it's easier and clean
<script type="text/javascript">
$(document).ready(function () {
$(function () {
$('#Item_Code').focus();
});
});
</script>

In this script block
<script type="text/javascript">
$(document).ready(function ()
{
var str = ('#ViewData["ControlView"]'); //alert(str);
if (str == "1")
ShowProduct();
else
ShowGrid();
});
var message = ('#ViewData["Success"]');
if (message == "Product Code Already Exits.")
{
document.getElementById("Item_Code").value ="";
document.getElementById("Item_Code").focus();
}
declaration and using message variable and document.getElementById function call not in ready function, so if declaration of yours textbox after this then in call document.getElementById("Item_Code") return null, because element with this id has not yet been rendered
for solve put code
var message = ('#ViewData["Success"]');
if (message == "Product Code Already Exits.")
{
document.getElementById("Item_Code").value ="";
document.getElementById("Item_Code").focus();
}
in ready function like this:
$(document).ready(function ()
{
var str = ('#ViewData["ControlView"]'); //alert(str);
if (str == "1")
ShowProduct();
else
ShowGrid();
var message = ('#ViewData["Success"]');
if (message == "Product Code Already Exits.")
{
document.getElementById("Item_Code").value ="";
document.getElementById("Item_Code").focus();
}
});

Related

using functions across multiple scripts/functions

I have a handful of js scripts in jquery:
vars.js
$(function(){
function yesOrNo(val){
if (val == "yes"){
return "yes";
}
if (val == "no"){
return "no";
}
});
scripts1.js
/// include(vars.js) <-- what would go here?
$(function(){
var var1 = "yes";
var test1 = yesOrNo(var1);
alert(test1);
});
scripts2.js
/// include(vars.js) <-- what would go here?
$(function(){
var var2 = "no";
var test2 = yesOrNo(var2);
alert(test2);
});
scripts3.js
/// include(vars.js) <-- what would go here?
$(function(){
var var3 = "yes";
var test3 = yesOrNo(var3);
alert(test3);
});
How can i call the function yesOrNo from a different scripts.js page?
If you want to add another file add type="module" parameters in your main code, then add the line import {yesOrNo} from './vars.js'; in your code.
Then, in the vars.js file, add the function as export function yerOrNo, not function yesOrNo.
My edit of your code:
vars.js
export function yesOrNo(val){
if (val == "yes"){
return "yes";
}
if (val == "no"){
return "no";
}
}
main.js
import {yesOrNo} from './vars.js'
$(function(){
var var2 = "no";
var test2 = yesOrNo(var2);
alert(test2);
});
On your html call main.js like that
<script src="main.js" type="module"></script>

jQuery/JavaScript operator equal SQL LIKE %Example%

Is there any way to search a specific part on my input with Javascript/jQuery?
Tried to do with 2 different ways, but no results for that
<script type="text/javascript">
$("#button").click(function () {
$("#DivToToggle").toggle();
var campo = document.getElementById('FieldToSearch');
if (campo.contains("Test")) {
document.getElementById('FieldToWrite').value = "123";
}
else {
document.getElementById('FieldToWrite').value = "456";
}
});
and
<script type="text/javascript">
$("#button").click(function () {
$("#DivToToggle").toggle();
var field = document.getElementById('FieldToSearch');
if (field.match(/Test.*/)) {
document.getElementById('FieldToWrite').value = "123";
}
else {
document.getElementById('FieldToWrite').value = "456";
}
});
#EDIT
Thanks to #TalhaAbrar, found the correct way to do it.
<script type="text/javascript">
$("#button").click(function () {
var field = document.getElementById('FieldToSearch').value;
if (field.indexOf("Test")) {
$("#DivToToggle").toggle();
document.getElementById('FieldToWrite').value = "123";
}
else {
$("#DivToToggle").toggle();
document.getElementById('FieldToWrite').value = "456";
}
});
Try to fetch the value of search field like document.getElementById('FieldToSearch').value and then use function indexOf in your if statement, that should work for you.
example code:
var field = document.getElementById('FieldToSearch').value;
if (field.indexOf("value_to_check")) {
// true if exists
} else {
// false if doesn't
}
I think you need to check the value of the field, e.g.
var campo = document.getElementById('FieldToSearch').value;
...
And then use includes(), e.g.
if (campo.includes("Test")) {
...

Strophe.js Notification Received (Composing)

I am trying to get notified when typing.
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="/js/strophe.min.js"></script>
connection.addHandler(onNotificationReceived, null, "message", "chat", null, null);
function onNotificationReceived(msg)
{
var composing = $(msg).find('composing'),
paused = $(msg).find('paused'),
active = $(msg).find('active'),
jid = $(msg).attr('from');
if (composing.length > 0)
{
$('.chat-feedback').css('display', 'block');
alert(1);
}
if (paused.length > 0)
{
$('.chat-feedback').css('display', 'none');
alert(2);
}
if (active.length > 0)
{
$('.chat-feedback').css('display', 'none');
alert(3);
}
return true;
}
But it does not work :(
Please help!
PS.
Сomplete script http://sitechat.ru/tests/chat1/
You need to parse the response as XML in onNotificationReceived(msg)
var msg = $.parseXML(msg);
And I think you meant
jid = $(msg).find('message').attr('from');

How to update text inside Div?

Iam trying to update Div text based on js variable null or not.
This is the code used for checking var userName is null or not.
If its null i dont want to do anything.But if userName is not null then i need to append ',' to existing text inside div .
$("document").ready(function (){
var userName = "";
if (userName == undefined || userName == null) {
alert('empty');
} else {
alert('not empty');
var div = document.getElementById('title b');
div.innerHTML = div.innerHTML + ',';
}
});
my html of the div
<div id="title" class="box">
<b>Welcome</b>
</div>
For example if userName is not null then i want div text as "Welcome ,"
if userName is null then i want div text as "Welcome"
Since you're using jQuery already...
$(document).ready(function (){
var userName = ""; // this will never get into the following if statement, always else
if (userName == undefined || userName == null) { // use if (!userName) as shorthand.
alert('empty');
} else {
alert('not empty');
var div = $('#title > b');
div.text(div.text() + ',');
}
});
You're using an odd mix of POJS and jQuery here. Here's a purely jQuery solution:
$("document").ready(function (){
var userName = "";
if (userName == undefined || userName == null) {
alert('empty');
} else {
alert('not empty');
$('#title b').append(',');
}
});
It's a little odd that you're declaring userName as an empty string within your function - the first if condition will never execute.
A more jQuery answer, if you are interested:
var userName = "";
//var userName = "User"; // for testing
//var userName; // for testing
//var userName = null; // for testing
$(document).ready(function (){
var greeting = !!userName ? "Welcome, "+ userName + "!" : "Welcome";
$("#title b").html(greeting);
})
jsFiddle

javascript jquery errors unable to resolve

I've been getting some errors with my javascrript and i can't figure out
what is wrong. I just made a simple jQuery statement to check if an email field
had a default value and if so, clear the field
Here is the jsFiddle link to the code i am working with: http://jsfiddle.net/qLfBH/
I can't figure out why the jslint is complaining about the line 5 "missing semicolon" and such.
$(document).ready(function () {
var email_default = "Enter your email address...";
$(':input').filter('[type="email"]').attr('val', email_default).focus({
if ($(this).val() == email_default) ;{
$(this).attr('val', ' ');
}
});
});
I have also tried using firebug to debug, but the javascript would not show in the scripts panel- probably because of some syntax error. Any help would be much appreciated.
UPDATE
I edited the code but its still complaining about a "missing semicolon" (see link to fiddle http://jsfiddle.net/qLfBH/3/)
$(document).ready(function () {
var email_default = "Enter your email address...";
$(':input').filter('[type="email"]').attr('value', email_default).focus({
if ($(this).val() == email_default) {
$(this).attr('val', ' ');
}
});
});
FIXED
Ok thanks for the help everyone. I forgot the "function" under the focus. I corrected the errors below and its working :)
$(document).ready(function () {
var email_default = "Enter your email address...";
$(':input').filter('[type="email"]').attr('value', email_default).focus(function () {
if ($(this).val() == email_default) {
$(this).attr('val', ' ');
}
});
});
Several issues. Here's a cleaned up version:
var email_default = "Enter your email address...";
$(':input[type="email"]').val(email_default).on('focus', function () {
if ($(this).val() == email_default) {
$(this).val(' ');
}
});
jsFiddle example
Note, you could also forgo the jQuery and use HTML5's placeholder attribute with just:
<input type="email" placeholder="Enter your email address...">
Remove the semi colon after you if statement
FROM
if ($(this).val() == email_default) ;{ // <----SHOULDNT BE THERE
$(this).attr('val', ' ');
}
TO
if ($(this).val() == email_default) {
$(this).attr('val', ' ');
}
See updated :
$(document).ready(function () {
var email_default = "Enter your email address...";
$('input[type="email"]').val(email_default).focus(function () { //<-- add function()
if ($(this).val() == email_default) { //<-- removed semicolon here
$(this).val(' '); //<-- either .attr('value',' ') or .val(' ')
}
});
});
Sample
Replace your JavaScript code with this
$(document).ready(function () {
var email_default = "Enter your email address...";
$('input').filter('[type=email]').val(email_default).focus(function() {
if ($(this).val() == email_default) {
$(this).val('');
}
});
});

Categories

Resources