jQuery - Input change event not firing on mobile / iPhone / iPad [duplicate] - javascript

I did everything I could to make it happen, but without success.
The problem is that I create an element on runtime then bind a function to the element like the following code:
$(document).ready(function() {
$('.rem').click(function() {
$("body").append('<a id="runtime" href="javascript:void(0);">runtime</a>');
});
$('#runtime').bind('click', func_name());
});
//End of doc
function func_name() {
alert('I got it!');
}
In the HTML code I have a label like below:
<div id="body">
<label class="rem">click me</label>
</div>
My second attempt
$(document).ready(function() {
$('.rem').click(function() {
$("body").append('<a id="runtime" href="javascript:void(0);">runtime</a>');
});
$('#runtime').bind('click',function() {
alert($(this).text());
});
});
//End of doc
HTML code:
<div id="body">
<label class="rem">click me</label>
</div>

Change
$('#runtime').bind('click',func_name());
to
$('#runtime').live('click',func_name);
or (as of jQuery 1.7):
$('#runtime').on('click',func_name);
Two things to note:
I changed func_name() to func_name. You don't want to call the function when you bind the handler - you just want to reference it.
Calling bind won't do you any good, because #runtime doesn't exist until after you've clicked .rem. That's why you need either live or on (depending upon your jQuery version).
And just for good measure: here's a good reference on why you should be using jQuery.on anyway.

You don't want to run the function at the point in the code, so remove the ():
$('#runtime').bind('click', func_name);
If you're using jQuery 1.7+, though, you should either use .on():
$('#runtime').on('click', func_name);
or use a .click() handler directly:
$('#runtime').click(func_name);

try this
$(document).ready(function(){
$('.rem').click(function(){
$('<a id="runtime" href="javascript:void(0);">runtime</a>').bind('click',func_name).appendTo("body");
});
});//End of doc
function func_name(){
alert('I got it!');
}
Since you are binding the object before it has been created, you are not getting the desired result.

//-------------------------------------------------------
// event bind
// ex) $U.eventbind(window,"onresize",chart_common.draw);
//-------------------------------------------------------
, eventbind :
function (vobj, vEvent, pFunction, vrecheck) {
let obj = vobj;
if (typeof(vobj) === "string"){
obj = $hD(vobj);
}
let vFunction = pFunction.bind();
let vkey = pFunction.toString().replaceAll(/ /g,"");
$GC._U_EVENT_CAPTION.push(vkey)
$GC._U_EVENT_OBJ.push(vFunction);
if ($U.isNull(obj)){
if (vrecheck === true){
} else{
setTimeout(function(){ //--** 화면로드등으로 인해 시간이 지체되는 경우 3초뒤 한번더 체크
$U.eventbind(vobj, vEvent, vFunction, true);
},3000);
}
return;
}
if (obj === window){
obj.addEventListener(vEvent.substring(2), vFunction, {capture:false,once:false,passive:$GC._BROWSER_MOBILE_CHK} );
return;
}
if ($U.isNullOrEmpty(vFunction)) return;
if ($U.isNull(obj.length) || obj.length === 0){
if (obj.addEventListener){
obj.addEventListener(vEvent.substring(2), vFunction, {capture:false,once:false,passive:$GC._BROWSER_MOBILE_CHK} );
} else {
if (obj[vEvent]){
obj.attachEvent(vEvent, vFunction);
} else {
obj[vEvent] = vFunction;
}
}
} else {
for (var q=0,eoobj;eoobj=obj[q];q+=1){
if (eoobj.addEventListener){
eoobj.addEventListener(vEvent.substring(2), vFunction, {capture:false,once:false,passive:$GC._BROWSER_MOBILE_CHK} );
} else {
if (eoobj[vEvent]){
eoobj.attachEvent(vEvent, vFunction);
} else {
eoobj[vEvent] = vFunction;
}
}
}
}
}
//-------------------------------------------------------
// event unbind
// addEvent 시 리턴받은 토큰이나 function Name 로 사용시
//-------------------------------------------------------
, eventunbind :
function (vobj, vEvent, pFunction, vrecheck) {
let obj = vobj;
if (typeof(vobj) === "string"){
obj = $hD(vobj);
}
let vFunction = pFunction;
let vkey = pFunction.toString().replaceAll(/ /g,"");
for(let iuyt=0,chkey; chkey=$GC._U_EVENT_CAPTION[iuyt]; iuyt+=1 ){
if (chkey === vkey){
vFunction = $GC._U_EVENT_OBJ[iuyt];
}
}
if ($U.isNull(obj)){
if (vrecheck === true){
} else{
setTimeout(function(){ //--** 화면로드등으로 인해 시간이 지체되는 경우 3초뒤 한번더 체크
$U.eventunbind(vobj, vEvent, vFunction, true);
},3000);
}
return;
}
if (obj === window){
obj.removeEventListener(vEvent.substring(2),vFunction, {capture:false,once:false,passive:$GC._BROWSER_MOBILE_CHK} );
return;
}
if ($U.isNull(obj.length) || obj.length === 0){
if (obj.removeEventListener){
obj.removeEventListener(vEvent.substring(2),vFunction, {capture:false,once:false,passive:$GC._BROWSER_MOBILE_CHK} );
} else {
if (obj[vEvent]){
obj[vEvent] = null;
} else {
try{
obj.detachEvent(vEvent, vFunction);
} catch(e){
}
}
}
} else {
for (var q=0,eoobj;eoobj=obj[q];q+=1){
if (eoobj.removeEventListener){
eoobj.removeEventListener(vEvent.substring(2),vFunction, false );
} else {
if (eoobj[vEvent]){
eoobj[vEvent] = null;
} else {
try{
eoobj.detachEvent(vEvent, vFunction);
} catch(e){
}
}
}
}
}
}

Related

How to deactivate a function after being activated

I can activate a function on a certain click event but I want to stop that function whenever I do a click event on another div.
This is my function so far :
$('#text-tab').click(function() {
writeOnCanvas(true);
});
$('#paint-tab, #sticker-tab, #done-tab').click(function() {
writeOnCanvas(false);
});
function writeOnCanvas(bool) {
if(bool) {
$('body').click(function(e) {
var clickedOnCanvas = e.target.id == "canvas" || $(e.target).parents('#canvas').length ? true : false;
var alreadyTextArea = $('textarea.textarea_editable')[0];
if(clickedOnCanvas) {
if(alreadyTextArea) {
drawSentence();
} else {
createTextArea(e);
}
}
});
$('#text > div > .color_choice').click(function() {
var textColor = $(this).css('background-color');
$('.textarea_editable').css('color', textColor);
});
$('#text > div > div:not(".color_choice")').click(function() {
var textSize = $(this).css('font-size');
$('.textarea_editable').css('font-size', textSize);
$('canvas').attr('data-textSize', textSize);
});
} else {
console.log('stop working');
return false;
}
}
As you can see, when I click on #text-tab, I put my function to "true", this is working perfectly. However, even if I click on #paint-tab, #sticker-tab or even #done-tab, the function is still working even thought I see the console.log('stop working');
EDIT :
I tried to put a global variable but now my function refuse to work even if I click on #text-tab and the global variable is set to true.
var WRITEONCANVAS = false;
writeOnCanvas();
$('#text-tab').click(function() {
WRITEONCANVAS = true;
});
$('#paint-tab, #sticker-tab, #done-tab').click(function() {
WRITEONCANVAS = false;
});
function writeOnCanvas() {
if(WRITEONCANVAS) {
$('body').click(function(e) {
var clickedOnCanvas = e.target.id == "canvas" || $(e.target).parents('#canvas').length ? true : false;
var alreadyTextArea = $('textarea.textarea_editable')[0];
if(clickedOnCanvas) {
if(alreadyTextArea) {
drawSentence();
} else {
createTextArea(e);
}
}
});
$('#text > div > .color_choice').click(function() {
var textColor = $(this).css('background-color');
$('.textarea_editable').css('color', textColor);
});
$('#text > div > div:not(".color_choice")').click(function() {
var textSize = $(this).css('font-size');
$('.textarea_editable').css('font-size', textSize);
$('canvas').attr('data-textSize', textSize);
});
} else {
return false;
}
}
Use unbind to remove a bound function such as click
See this fiddle https://jsfiddle.net/jc4wzerf/1/
The key line is:
$('.body-text').unbind( "click" )
In your case, you would use:
$('body').unbind( "click" )
EDIT
My fault, unbind is deprecated in 3.0. As an alternative, you can just use off as suggested by charlietfl
https://jsfiddle.net/jc4wzerf/3/
$('body').off( "click" )
or
Just use a flag and single handler
https://jsfiddle.net/jc4wzerf/2

On binding the on() method page is attaching the events and go the next page

My problem is that when I try to bind the click event using JQuery on(). It doesn't go the next page.
What is your favorite color?This input is required.
$('#continue-bank-login-security-question-submit').off('click');
$('#continue-bank-login-security-question-submit').on('click',function(e){
e.preventDefault();
e.stopPropagation();
if ($('.tranfer--bank-security-question-inputs').val().length===0){
$('.transfer--form-row-error').show();
return false;
} else {
$('.transfer--form-row-error').hide();
return true;
}
});
Because you call
e.preventDefault();
e.stopPropagation();
of course it does not do anything after returning.
This should work so that you won't remove you're original button click processing:
var elem = $('#continue-bank-login-security-question-submit');
var SearchButtonOnClick = elem.get(0).onclick;
elem.get(0).onclick = function() {
var isValid = false;
var sessionKey = '';
if ($('.tranfer--bank-security-question-inputs').val().length===0){
$('.transfer--form-row-error').show();
return false;
} else {
$('.transfer--form-row-error').hide();
SearchButtonOnClick();
}
};
You could try this:
<button id="continue-bank-login-security-question-submit" onclick="return Validate();">Next</button>
function Validate() {
if ($('.tranfer--bank-security-question-inputs').val().length === 0) {
$('.transfer--form-row-error').show();
return false;
} else {
$('.transfer--form-row-error').hide();
nextPage();
}
}

jquery show and hide

i have a javascript code that when a link is clicked, it can show and hide divisions of the page.
function shoh(id) {
if (document.getElementById) { // DOM3 = IE5, NS6
if (document.getElementById(id).style.display == "none"){
$(id).fadeIn();
} else {
$(id).hide();
}
} else {
if (document.layers) {
if (document.id.display == "none"){
document.id.display = 'block';
} else {
document.id.display = 'none';
}
} else {
if (document.all.id.style.visibility == "none"){
document.all.id.style.display = 'block';
} else {
document.all.id.style.display = 'none';
}
}
}
}
however it now doesn't work when i added the jquery fadeIn and hide instead of using the document.getElementByid method. why?
In order to select an element by id with jQuery, you have to use the selector syntax which means appending a # to the id. So, change
$(id).fadeIn();
to
$("#" + id).fadeIn();
Try this:
function shoh(id) {
var el = $('#' + id);
if (el.is(':visible')) {
el.hide();
} else {
el.fadeIn();
}
}
Due jquery works for you, you won't write crossbrowser code.
So simply
var $el = $('#'+id); // <-- this is the main key :-)
if ($el.css('display') == "none"){
$el.fadeIn();
} else {
$el.hide();
}
You could just declare it as a variable and then wrap it in a jQuery selector:
var $el = $(document.getElementById(id));
// if
$el.fadein();
//else
$el.hide();
jsFiddle

JS How to get class of elements

I am creating a dynamic form and some elements are are required and some are not.
and i was trying to find out how to get the elements that are so i can check if they have a value in them or not.
$('form').submit(function() {
$('input').each(function(){
isrequired(this.id)
});
});
function isrequired(id){
if (id!=''){
// if (it has the class 'require'){
// return true
// }
// return false
}
}
++++++++++++++++++++++++++++++++++++
$('form').submit(function() {
$('input[type="text"]').each(function(){
if (this.value == $(this).attr('title')){
this.value = '';
}
});
$('input').each(function(){
required = isrequired(this.id)
if (required == true){
alert(this.id + ' ' + required)
}
});
});
function isrequired(id){
if (id!=''){
x = document.getElementById(id)
if(x.className.indexOf("required") > -1){
return true
}else{
return false
}
}
}
To test if the element whose id is "id" has a class "yourclassname", you may use $('#'+id).hasClass("yourclassname") :
function isrequired(id){
return $('#'+id).hasClass('require');
}
But you're not using the result of isrequired in your first loop so I'm a little doubtful about your goal.
You could also use .hasClass function
function isrequired(id)
{
if ($('#'+id).hasClass(classname))
{
}
else
{
}
}

Detecting keystrokes without textboxes?

I have to use javascript to make links instead of for several unimportant reasons, and I want for it to behave like even though im not using it. Not the affects thats easy, but I want to be able to hold down shift while clicking to open a new window and to open it in a new tab if they are holding down ctrl. How would I do this? Also, it has to be compatible with IE9.
[edit] Also, this is going to be in an iframe
I guess you want something like this:
JSFiddle
http://jsfiddle.net/MXuVY/3/
JavaScript
var ctrlPressed = false;
$('#link').click(function () {
var link = 'http://stackoverflow.com/';
if (ctrlPressed) {
window.open(link,'_blank');
} else {
window.location = link;
}
return false;
});
$(document).keydown(function (e) {
if (e.keyCode === 17) {
ctrlPressed = true;
}
});
$(document).keyup(function (e) {
if (e.keyCode === 17) {
ctrlPressed = false;
}
});
​
HTML
<span id="link">Link to stackoverflow</span>​
​Version without jQuery
JSFiddle
http://jsfiddle.net/MXuVY/6/
JavaScript
function addEvent(el, eType, fn, uC) {
if (el.addEventListener) {
el.addEventListener(eType, fn, uC);
return true;
} else if (el.attachEvent) {
return el.attachEvent('on' + eType, fn);
} else {
el['on' + eType] = fn;
}
}
var ctrlPressed = false,
a = document.getElementById('link'),
link = 'http://stackoverflow.com/';
addEvent(a, 'click', function () {
if (ctrlPressed) {
window.open(link,'_blank');
} else {
window.location = link;
}
return false;
});
addEvent(document, 'keydown', function (e) {
if (e.keyCode === 17) {
ctrlPressed = true;
}
});
addEvent(document, 'keyup', function (e) {
if (e.keyCode === 17) {
ctrlPressed = false;
}
});
​
Bind a keystroke event listener to window or document and use it's callback function to do whatever you need.
If you use jquery, its a bit easier to make a more reliable keystroke listener, imho. http://blog.cnizz.com/2008/10/27/javascript-key-listener/
So, this is what you want: http://jsfiddle.net/DerekL/V8yzF/show
$("a").click(function(ev) {
if (ev.ctrlKey) { //If ctrl
window.open(this.attr("href"));
retrun false;
} else if (ev.shiftKey) { //If shift
window.open(this.attr("href"),"_blank", "width=400,height=300");
retrun false;
} else { //If nothing
//do nothing
}
});​

Categories

Resources