Clear input textbox programmatically [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
jQuery Chosen reset
I'm using Chosen plugin on my project.
http://jsfiddle.net/tt13/gFzzc/
The problem is, I can't reset chosen input textbox programmaticaly. How to do it when I click clear button?

This works jsfiddle
$('button').click(function(){
$('.search-choice-close').click();
});
You might narrow down the scope a bit in case of multiple controls.

First of all, you should use the correct version of Chosen, taken from here:
https://raw.github.com/harvesthq/chosen/master/chosen/chosen.jquery.js
Then, simply reset the value of the select and trigger "liszt:updated" event:
$("button").on("click", function() {
$(".chzn-select").val("").trigger("liszt:updated");
});​
DEMO: http://jsfiddle.net/gFzzc/13/

You can try something like this :
Give Id to the button:
say clear_chosen
and then trigger close button:
javascript code::
$("#clear_chosen").click(function(){
$(".search-choice-close").click();
});
Write the code inside this block::
$(function () {
$('.chzn-select').chosen();
});

Ugly hack, but works:
Demo: jsfiddle
Code:
$('button').click(function() {
$(".chzn-select").next('.chzn-container').find('.search-choice').remove();
$(".chzn-select").val('');
$(".chzn-select").next('.chzn-container').find('.search-field input').focus();
});

Related

JQuery doubleclick listener in select - IE not working [duplicate]

This question already has answers here:
double click using IE
(2 answers)
Is there a workaround to trigger an option.click event in IE?
(1 answer)
Closed 8 years ago.
I seem to have a bit of trouble with Internet-Explorer once again...
The following code works fine in other browsers but IE doesn't like it at all!
It would be a simple double click listener for each option in a select.
Here's the simplified JSFiddle
http://jsfiddle.net/zoq8yo0s/1/
Any suggestions?
function registerClickListener() {
$(".available-option").each(function(){
$($(this)).dblclick(function(){
alert("dblclick");
});
});
}
Thanks! :)
Bind the dblclick event to the select element its working,
$(document).ready(function () {
$("#selectEl").dblclick(function () {
alert("dblclick");
});
});
Fiddle
You could start a timer on the click of an element and wait for a while for the second click. If second click comes, clear the timer and run double click. If the second click doesn't come, then it is a single click.
if .available-option is the id then use #available-option instead of .available-option
Hope will help u.
try this
$(document).on("dblclick",".available-option", function (){
//code here
});
or you can do this :
$(document).on("dblclick",".available-option", myfunction);
function myfunction(){
//code here
}

Alert when dynamic button is clicked [duplicate]

This question already has answers here:
jquery - Click event not working for dynamically created button [duplicate]
(4 answers)
Jquery/Javascript: Buttons added dynamically with javascript do not work
(3 answers)
Closed 8 years ago.
I can't get an alert to trigger when a dynamically generated button is clicked?
The alert is not going to be the final function but was a test to make sure the trigger is working correctly.
I tried a "onclick" function as a trigger and used the id as a jQuery trigger so not sure why it would not work; I will add a snippet below that shows what I mean.
From the file that generates and displays the button (it displays OK):
var modOptsMsg = document.getElementById("modOptionsMessage").value + '<input type="button" id="removePost" onclick="removePost()" value="Remove Post"/>';
$("#modOptsShowMsg").empty().append(modOptsMsg);
Neither of these simple tests work in JS or jQuery:
function removePost(){
alert("alert");
}
$('#removePost').click(function(){
alert("alert");
});
As #Regent has pointed out in the comments, use:
$(document).on("click", '#removePost', function() {
alert("alert");
});
$('#modOptsShowMsg').on("click", '#removePost', function() {
alert("alert");
});
the above code will work..if jquery is lesser than 1.7 use .live() instead of .on()

How can I stop <a> tag jumping to the target which is specified by href attribute? [duplicate]

This question already has answers here:
jQuery disable a link
(18 answers)
Closed 8 years ago.
I know that Link will just do the job,
but what I intend to do is to specify a href, but will not jump to it when being clicked, just like:
Link
When I click on this link, I'd just like the onclick is invoked, whereas href is just a 'show' without jumping to http://mydomain.com.
How could this be done by javascript or css? Thanks a lot!
There are a few ways you can do this. If you must fulfill the answer within the HTML then:
click here
But better practice is to follow Andrew Spartar's solution and separate the JavaScript using a class or other selector.
add class to the link, for example: class="not-jump"
then in jQuery add listener:
$( ".not-jump" ).click(function(e) {
e.preventDefault();
});
That should do the trick.
like this:
Link
JS:
function myFunc(e) {
e.preventDefault();
...
}
and to separate html from js:
Link
JS:
$(".somelink").on('click', function(event) {
event.preventDefault();
});

why does the jquery change event not trigger though the value of input changed indeedly? [duplicate]

This question already has answers here:
Why does the jquery change event not trigger when I set the value of a select using val()?
(10 answers)
Closed 8 years ago.
JSFIDDLE
Html Code:
<body>
<input value="123">
<button>button</button>
</body>
Js Code(using JQuery 1.2.0):
$(function(){
$("button").click(function(){
$("input").val("balabala...");
});
$("body").on("change","input",function(){
alert("change");
alert($("input").val());
});
});
Question:
At the beginning, the value of the input is "123".
When I change the text through editing by hands and click other places not the input field, the change event of the input will trigger.
However, when I click the button, which will also change the value of the input, the change event of the input will not work.
Thanks for any help.
The change event is not triggered when changing the value programatically with code, you have to trigger it yourself
$(function(){
$("button").click(function(){
$("input").val("balabala...").trigger('change');
});
$("body").on("change","input",function(){
alert("change");
alert($("input").val());
});
});
FIDDLE
JSFiddle
You need to append .change() onto the click function:
$(function(){
$("button").click(function(){
$("input").val("balabala...").change();
});
$("body").on("change","input",function(){
alert("change");
alert($("input").val());
});
});

How to get input has focus or not using jquery [duplicate]

This question already has answers here:
Closed 11 years ago.
How to get input tag of html has focus or not using jquery
keydown event will work for form if input,image etc. tag has focus. but it will not work it focus is on form but not on any tags like input,image etc.
How can I solve this.
Please help
Thanks in advance
$('#title').focusout(function()
{
if($(document.activeElement).is(":focus"))
{
alert('focus');
}
else
{
alert('not focus');
}
});
// this will not work
you can do it by
if ($("#id").is(":focus")) {
alert('focus');
}
Its much easier to check by using jquery.
Using jquery u can do it like this
if($"#div/inputtocheck").is(":focus"))
{
//do something u want if it is in focus
}
The :focus returns true if the div is in foucs and false if it is not in focus
You can use document.activeElement.

Categories

Resources