Calling function in JavaScript like in JQuery - php

I have a div:
<div id="test" style="width:auto; height:auto;">
</div>
And a function:
$(function() {
$("#test").attr("contentEditable", "true");
$("#test")
.attr("tabindex", "0")
.keydown(function(){ alert(1); return false; })
.mousemove(function(){ alert(2); return false; });
});
How can I implement this code in JavaScript without including the JQuery library?

You can do it like this in javascript without using jquery, Demo available here JsFiddle
You can put it in onload method of body then it will call onload of body or just put it in script section below all controls without putting it in function then it will call when document is ready like jquery method $().ready();
var test = document.getElementById('test');
test.setAttribute("contentEditable", "true");
test.setAttribute("tabindex", "0");
test.onkeydown = function(event) { alert("KeyDown");}
test.onmousemove = function(event) { alert("MouseMove");}

function runme() {
var elm = document.getElementById("test");
elm.contentEditable = "true";
elm.tabIndex = 0;
elm.onkeydown = function() { alert(1); return false; };
elm.onmousemove = function() { alert(2); return false; };
}
if(window.addEventListener)
window.addEventListener("load", runme, false);
else
window.attachEvent("onload", runme);

Adil has the right idea, but to improve upon it a bit, you could store the element in a variable so you do not have to make a call to get the element every time. So I would change it to look something like this:
var t = document.getElementById('test');
t.setAttribute("contentEditable", "true");
t.setAttribute("tabindex", "0");
t.onkeydown = function(event) { alert("KeyDown");}
t.onmousemove = function(event) { alert("MouseMove");}
Upvoted Adil for beating me to it and for providing the jsfiddle link :)
updated: nevermind, since you just updated your post

Related

If Jquery variable equals the class of a div then set display to block

I'm outputting several divs using php - each div has a different country name for the class.
I'm also outputting a variable using jquery which checks if content of a div has changed. That variable will also be a country name.
If the jquery variable matches a div class, I want that div to .show(), how can I do that?
This is my (horrible) attempt:
(function($) {
$(document).ready(function () {
$("#block1").bind("DOMSubtreeModified", function() {
message = $("#block1").html();
regiondiv = '.';
fullregion = regiondiv + message;
alert(fullregion);
if ($('.region-logos').hasClass(message)) {
$(fullregion).show();
} else {
$(fullregion).hide();
}
});
});
})(jQuery);
If I click on Canada for instance, it will show the .Canada div, but if I click on Spain after it, it's shows the .Spain div as well as the .Canada div. I.e once you click on more than one it just continues to show them all. It doesn't hide the divs if you haven't click on it.
var class = 'no-class';
$(document).ready(function () {
$("#block1").bind("DOMSubtreeModified", function() {
var regiondiv = '.';
var fullregion = regiondiv + class ;
$(".fullregion").hide();
class = $("#block1").html();
fullregion = regiondiv + class ;
$(".fullregion").show();
});
});
Not sure if I followed the question correctly. I assume you want to display a div if class of that div matches a variable in javascript. If so the you can try this:
(function($) {
$(document).ready(function () {
$("body").bind("DOMSubtreeModified", function() {
// Not sure how are you generating this jquery variable
var yourJqueryVariable;
// Check if class name exists
if ($(".yourJqueryVariable")[0]){
$(".yourJqueryVariable").show(); //or even $(".yourJqueryVariable").css('display','block');
}
});
});
})(jQuery);
(function($) {
$(document).ready(function () {
$("#block1").bind("DOMSubtreeModified", function() {
var message = $("#block1").html();
//ok so you have the variable to check
if($('.'+message).length > )) { //is there 1 or more of these classes in the DOM?
$('.'+message).each(function() { //there is, loop and show them all
$(this).show();
});
}
});
});
})(jQuery);
EDIT
Ok looking at your updated answer, assuming there are multiple .region-logos you need to perform your if else within a loop of those:
$('.region-logos').each(function() {
if($(this).hasClass(message)) {
$(fullregion).show();
}else {
$(fullregion).hide();
}
});

PHP/Jquery:refresh the captcha on click with jquery

I use a captcha in my login form and I want to refresh the captcha when I click the captcha itself!but it doesn't work!
my html code:
<cite class="fr">
<img id="captcha_img" src="securimage/securimage_show.php" alt="点击图片刷新验证码" />
</cite>
my jquery code:
$('#captcha_img').click(function(){
//alert("hh");
$('#captcha_img').attr("src","securimage/securimage_show.php");
});
I also search some docs about this question and try some other ways,but it still doesn't work,can someone give me some ideas?
Both methods below should work ( uncomment one by one and try it ):
jQuery(function ($) {
$('#captcha_img').on( 'click', function() {
// $(this).attr( "src","securimage/securimage_show.php?"+Math.random() );
// $(this).attr( "src","securimage/securimage_show.php?"+new Date().getTime() );
});
});
Browser is probably caching image. Try this:
$('#captcha_img').click(function() {
var imageUrl = 'securimage/securimage_show.php?' + new Date().getTime();
$(this).attr('src', imageUrl);
});
DEMO
$(function () {
$('#foo').click(swapImages);
var secondImg = 'http://digital-photography-school.com/wp-content/uploads/2013/03/Acorn256.png';
function swapImages() {
var img = $("<img id='foo' src='" + secondImg + "' />");
$(this).replaceWith(img);
$(img).click(swapImages);
}
});
$('#captcha_img').click(function(){
//alert("hh");
$('#captcha_img').attr("src","securimage/securimage_show.php");
return false;
});
Or use e.preventDefault() like
$('#captcha_img').click(function(e){
e.preventDefault();
$('#captcha_img').attr("src","securimage/securimage_show.php");
//return false;
});
tell me if that works. In this case, return false will work just fine because the event doesn't need to be propagated.

Using Colorbox and a javascript function

I'm trying to display a hyperlink that has a colorbox popup associated with it.
The javascript is:
function bid() {
var bid = document.getElementById("bid").value;
if (bid>0 && bid<=100) {
var per = 3.50;
} else if (bid>100 && bid<=200) {
var per = 3.40;
} else if (bid>200 && bid<=300) {
var per = 3.30;
}
var fee = Math.round(((bid/100)*per)*100)/100;
var credit = 294.9;
if (fee>credit) {
var message = 'Error';
} else {
var message = '<a class="popup" href="URL">The link</a>';
}
document.getElementById("bidText").innerHTML=message;
}
The javascript works fine and displays the link in the right conditions, the problem however is that when clicking the link, the Colorbox isn't being applied and the page loads as a normal hyperlink.
I have the following code in the header:
jQuery(document).ready(function () {
jQuery('a.popup').colorbox({ opacity:0.5 , rel:'group1' });
});
If I output just the hyperlink in the standard html source, it works fine and displays correctly in the Colorbox.
Any help would be greatly appreciated :)
You need to wait until you've appended the link before you call the colorbox() method on it.
Move your colorbox() method so that it comes after your innerHTML.
jQuery('a.popup').colorbox({ opacity:0.5 , rel:'group1' });
when adding html dynamically you, the event added already can not be triggered.
try the following code
jQuery(document).ready(function () {
$("a.popup").on("click", function(event){
applycolorbox($(this));
});
function applycolorbox($elem) {
$elem.colorbox({ opacity:0.5 , rel:'group1' });
}

JQuery after AJAX GET

I have this AJAX GET:
function edit(str){
ajax.onreadystatechange=function()
{
if (ajax.readyState==4 && ajax.status==200)
{
document.getElementById("editaircraftdialog").innerHTML=ajax.responseText;
$("#editaircraftdialog").dialog('open');
$("#loadingdialog").dialog('close');
}
}
ajax.open("GET","./edit_aircraft.php?icao="+str,true);
ajax.send();
$("#loadingdialog").dialog('open');
}
The JQuery code in edit_aircraft.php does not work. Here's an example:
<script>
$(function() {
$("#insertaircraft")
.button()
.click(function(event) {
});
});
</script>
I have had the same problem in the past and I cannot fix it.
You can not load JavaScript with innerHTML. It does not get evaluated.
Use jQuery to your advantage
function edit(str){
var loading = $("#loadingdialog").dialog('open');
var aircraft = $("#editaircraftdialog");
aircraft.load("./edit_aircraft.php?icao="+str, function(){
loading.dialog('close');
aircraft.dialog('open');
});
}

Grabing a data attribute of a clicked element?

I have a link that opens a modal when clicked, however that element also has a data-id="#id here" attribute that I need to grab so I can pass it to my php script for processing. How can I accomplish this?
My JS code:
$(document).ready(function() {
$('#edit_general').click(editModal);
});
function editModal(event)
{
var modal = $('#editModal');
modal.modal({
show: true
});
}
My html:
<li><a id="edit_general" href="#editModal" data-uid="<?php echo $u->id; ?>">Edit General</a></li>
I tried using the this keyword but it didn't work.
How can I accomplish this?
Try this: Demo http://jsfiddle.net/szS2J/1/ or http://jsfiddle.net/ug7ze/
$(this).data('uid')
Hope it fits the cause :)
like this:
$('#edit_general').click(editModal);
$(this).data('uid')
full
$(document).ready(function() {
$('#edit_general').click(editModal);
});
function editModal(event) {
alert($(this).data('uid'));
var modal = $('#editModal');
modal.modal({
show: true
});
}​
You can get the value using the attr method: $(this).attr('data-uid')
Like this stored in data_id.
$(document).ready(function() {
$('#edit_general').click(function() {
var data_id = $(this).data('uid');
var modal = $('#editModal');
modal.modal({
show: true
});
});
});

Categories