Formal way to perform an "onchange" using jquery? - javascript

I'm using jquery for the first time and I cannot get any examples for "onchange" actions to work.
Can someone explain the most basic and formal way to perform an "onchange" action using jquery. Here's my stats:
JQUERY VERSION
$().jquery
"1.7.2"
Example Activity That I Can't Get To Work
https://jsfiddle.net/k27pgkmr/
My jquery
$(document).ready(function(){
$("#hostnameList").on("change", function() {
contents = $('#hostnameList').val();
$("#Names").html(contents);
}); });
GOAL:
Have things happen when I select something from my dropdown menu.
What's the most graceful way to do this?

Nothing wrong with your code, but in your fiddle. You forgot to reference the jQuery lib. Also, I have set your fiddle to wrap the javascript in the <head> since you're already binding it to the $(document).ready().
Updated Demo

Related

input type range does not work inside angularjs repeat

I have problem in input type range to get it's value on change method. The problem is it si working if ti is outside of the ng-repeate. I have search a lot but find any solution yet. I attached Plnkr file where you can see the code and all:
https://embed.plnkr.co/pEKXfPiR1tNHD4G2yDmU/
There ware soem example that I found, I am adding here for reference to solve this problem.
HTML Range Slider and jQuery?
You are attaching your listeners before the DOM has been created. You need to use document.ready()
$(function() {
$('#chanceSlider0').on('change', function() {
$('#chance0').val( $('#chanceSlider0').val() );
});
});
DEMO
You are trying to mix jquery and angularjs which is not a good thing and is not recommended.
i would suggest you have a look at
https://github.com/angular-slider/angularjs-slider

Javarscript issues with dynamic tooltipster

I'm using Tooltipster which seems to be a nice jquery plugin.
Regardless I need to have my tooltips dynamic, which I don't believe should be that difficult. However I wrote a script and maybe it's because I'm tired or I don't know the simplest of javascript. Probably a combination of both.
I can't seem to get around this particular error. TypeError: $(...).tooltipster is not a function.
Here is the basic javascript code:
$("img#box_image[data-img-number]").hover(function(e) {
e.preventDefault();
i = $(this).attr("data-img-number");
var w = "http://trailerbrokerimages.s3.amazonaws.com/pics/" + i ;
window.console.log('before tool');
window.console.log('before tool ' +w);
tool(w);
});
var tool = function (w) {
$('.tooltip_two').tooltipster({content: $('<span><img style="height:191px; width:256px;"src="http://trailerbrokerimages.s3.amazonaws.com/pics/'+w+'" /></span>')});
An example of the code can be found at http://www.trailerbroker.com/go/listings/view/219
I suspect it's lame mistake on my part, thanks.
You have the same id box_image for multiple elements.
I understand that you're trying to make it unique by appending the data-img-number, but this won't work, as there's no way you can do this at run time unless your explicitly specifying different hover handlers.
Instead you could attach the hover handler to a class.
Add a class="box_image" to your <img /> elements and attach the hover as follows,
$(".box_image").hover(//rest of your code here//)
This should give you the desired functionality.
I solved this problem by using twitter bootstrap popover. Don't waste your time with tooltipers.

What jquery selectbox plugin is used in trulia.com?

Does any one know what jquery plugin was used in this page of trulia.com in the refine search section. I think this is a good selectbox plugin and i've googled it and with no luck. I would really like to use this. Thanks in advance.
I have peeked into the code on trulia.com, and it does not look like they are using a plugin. It is simply an ordinary selectbox, which is styled with wrapper elements.
There are some code in the polarbear-home.js javascript file, which sets/removes focus to the select.
$(".select-wrapper>select").bind("focusin", function (c) {
$(this).prev().addClass("focus")
});
$(".select-wrapper>select").blur(function (c) {
$(this).prev().removeClass("focus")
});
But, my guess is, that this is no public available plugin, but some wrapper styling, and some some js/jquery code to do the focus/unfocus styling.
Try out SelectBoxIt. I just released it last week. DailyJS also wrote about it here http://dailyjs.com/2012/04/24/jquery-roundup/

Detect checkbox change in jstree

I am using the latest jstree commit from github with the checkbox plugin as a part of my form. I am using the tree with the "real_checkboxes" attribute.
Everything is fine except the checkbox plugin does not actually add any changed property attribute to the hidden field and nor does it seem to have a external function that will allow me to hook in to create custom functionality.
Is it possible for me to understand and listen for when a checkbox is either ticked or unticked?
Thanks,
UPDATE: after doing some experimenting I was able to over ride the default functionality of the check and uncheck methods using:
$.jstree._instance.prototype.check_node = function(node){ alert("here"); }
However it isn't very clean and it does override the whole method.
Is there:
a) a cleaner way to do it?
b) a way to just do a callback on the function rather than replacing the whole damn thing?
Thanks again,
#Noctyrn
Yes I did that originally but then I looked through the docs a lot more closely and found this:
$(".js_tree_'.$this->attribute.' div").bind("check_node.jstree", function(){});
But yes your function does the same :). But since mine is right to the jsTree docs Ima mark my answer as the right one. Also that function allows for different trees on the same page to have different binds so it is better overall :).
Thanks for the help :),
This is actually less clean but at least it gets the job done:
var check_node_func = $.jstree._instance.prototype.check_node;
$.jstree._instance.prototype.check_node = function(node) {
check_node_func.apply(this, arguments);
alert("here");
}

jquery ui.slider: add click event to display/hide handle

I am completely new to javascript and jquery. My programming knowledge is... nonexistent, I just started some days ago with some simple tasks like replacing a CSS class or toggling a div. So I want to apologize if I'm treading on someones toes by asking newbie-questions in here. But I hope that someone can help me and solve my problem.
I need to implement some sort of visual analog scale for a survey; ui.slider is perfect for that one. But I need the handle to be hidden by default. When the user clicks on the slider, the handle shall appear in the proper position. That should be fairly simple - at least I hope so - by just hiding the handle with CSS and changing it by a click event on the slider.
I use the following piece of code to wrap a normal div (a div is needed in my understanding to apply the jquery slider.js) to my input elements (they should be - at least visually - replaced by the slider) and pass the value of the slider to the input elements (needed for passing the values to a form). that works properly. (I do that instead of just putting a div in my DOM by default because I cannot influence some PHP scripts that will generate form elements of the survey and so on)
$(function () {
$.each($('.slider'),
function () {
obj = $(this);
obj.wrap('<div></div>');
obj.parent().slider({
change: function (event, ui) {
$('input', this).val(ui.value);
}
});
});
});
Hiding the slider-handle can be done by CSS as described above by changing style properties of a.ui-slider-handle. but when I add a normal click event to the slider (.ui-slider) that changes CSS properties of the handle, nothing happens. As far as my basic knowledge goes it should have something to do with the click event not working on generated DOM elements. Am I right with that one? And if yes: how can I solve this problem? Could someone provide me a piece of code for my function and explain it so I might comprehend what's exactly going on?
I read a tutorial about events on learningjquery.com but I have not made enough progresses the last few days since I started working with JS/jquery to comprehend the steps and translate it into my example/problem. And I am running out of time (I need this for a survey I have to make asap, that's why I hope someone could give me a hint so I can solve this little issue somehow).
Any reason you can't just include the show on the change event rather than a click? It's a bit cleaner code-wise rather than including a whole new event.
$(function() {
$('.slider').wrap('<div></div>').parent().slider({
change: function(event, ui) {
$('input', this).val(ui.value);
$('.ui-slider-handle').show();
}
});
});
Also, there was a bit of redundancy in the code - most jQuery functions return the object itself, so you can chain them. And you don't need that each function, since most jQuery functions also, when applied to a collection, run on all of them :)

Categories