Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am new to phonegap.So,using JAVASCRIPT for the first time.I am working in javascript. I am comfortable working with Java Script. I am implementing a plugin for SMS in Phone Gap.
In the tutorial they have asked to use the following code.
window.plugins.sms.send($('#phone').val(),
$('#message').val(),
function () {
alert('Message sent successfully');
},
function (e) {
alert('Message Failed:' + e);
}
);
Could some one help me out in de-coding the jquery code?
please help me out.
You can use document.getElementById('phone').value instead of $('#phone').val() to get element's value without jquery.
It looks like that function window.plugins.sms.send takes a phone number, a message to send, a function to run on success, and a function to run on failure. What that code is doing is providing the first 2 of those 4 things using jQuery selectors, and the last 2 as anonymous functions.
Have a look at http://jquery.com/ (yes, just the homepage) and scroll down to the "A Brief Look" section. That will give you an idea of what the code you provided is doing.
Basically, the tricky bit to get your head around is that $ in there.
The $ in jQuery is just the function that instantiates jQuery. Anything in the parentheses immediately after it is a selector, and then everything after that are methods to call on the object returned by the selector.
The code you provided is looking through the DOM for an element called phone and an element called message. The # in front of those means to match only one, unique element. jQuery selectors can also be used to select groups of elements and operate on them all at once.
I'd really recommend going through the jQuery tutorials as they are quite quick to get through. If you already know javascript you'll have little problem getting your head around it.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Fairly new to this. I have a function called "getInformation" with 3 ajax functions in it.
When the if statement fails in the success function shown here, I want to call/trigger the "getInformation" function, but I don't know which selector to use (or if I even need a selector).
success: function(serverResponse){
if(serverResponse.length>1){
try{
var searchResultsHTML='';
console.log(serverResponse);
$("#searchResults").append(searchResultsHTML);
}catch (ex) {
console.error(ex);
$("#searchResults").text("An error occurred processing the data");
}
} else{
//run the other ajax calls
$.fn.getInfortmation();
}
I believe you may be misunderstanding the meaning of the $ in jQuery. This is actual just a shorthand or alias of sorts for the word jQuery, a class (or some JS equivalent holder) that contains all of the functions in the library.
Just because you are writing code within a jQuery callback (the success handler), does not mean you need to use only jQuery functions. You are free to use your own code, and I believe that getInformation is one of your defined functions.
To solve the problem, call the function without the jQuery $ like fn.getInformation. I assume that your function is within a different object called fn. If not, remove the fn as well.
As far as selectors go, thats a different topic. Those are used to easily select and work with HTML elements on the page in your code. jQuery is very useful for doing this, but not all jQuery functions involve selectors/DOM. For example, in this case it appears you might be doing some AJAX which directly doesn't have anything to do with manipulating page elements but rather is about communicating data to a server.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i mean i wanna iterate manually using a for-loop or something. but this piece of code i came up with seems to be not working. i like combining javascript with jquery since jquery is not my cup of tea for major projects. i don't know much jquery either, i would say I'm beginning to learn, though. how do you iterate over a nodelist in jquery is the question i have for all those jquery fans this time. is it similar to the javascript way? anyway this is what i have come up with (the code of a beginner).
$("sn"[i]).fadeIn();
$("sn"[i]) the part which failed, according to google chrome.
try this:
$("sn[" + i + "]").fadeIn();
I think you mean that "sn" is the selector for the nodes, in that case:
$("sn").fadeIn();
This works on all the elements that match the selector, jQuery will do the iteration. However if you want to select all elements that have the 'sn' class you should prefix the selector with a . like so: ".sn"
if you want to loop manually try:
$(".sn").each(function(i) {
$(this) // do some magic with the individual element here
});
See more on iterating with each here:
https://api.jquery.com/each/
Assuming sn is a variable containing the node list, you are probably looking for
$(sn[i])
or
sn.eq(i)
if sn is already a jQuery object.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Sometimes it occurs that I like a feature on an page and want to read its javascript,
but it is an difficult task to manually find where the function is especially when the webpages more than one js files.
Is there a way I can get/copy the only required js file by its name which I can easily get by inspecting page.
examole:
<div id='abc()'></div>
Now how to fine the source of function div()?
The simplest solution for a function which is defined in the global scope :
open the console of your browser (F12 in most browsers)
type the name of the function
For example type klass in the console on this page.
In the general case, you really need to learn to use the console, including the debugger.
Regarding your practical question : what changes the background of the http://www.noisli.com/ page :
it's not the changebackground function : this function doesn't exist
it's in the first line of jplayer.js
But the way I found it is too hacky, I hacked the Math.random function by typing this in the console :
Math.random = function(){ debugger; return Math.random() }
so that I would enter in debug and see the call stack...
I hope somebody can propose a clean way.
If you can execute boormakrlets in your address-bar, you could do:
javascript:abc.toString()
Or better:
javascript:alert(abc.toString())
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I've been writing Javascript with jQuery for a while, I can make it do what I want, but I've never written anything really reusable or modular. Now it's time for me to take that step and write proper reusable Javascript.
I thought I'd start with something I've implemented countless times, a confirm delete dialog. I want to be able to specify a function to execute on confirm and a function to execute on cancel.
The way I see this working (and this is open to criticism) is to do something like:
$(element).confirmDialog(function(){
// this is the cancel callback
},
function(){
// this is the confirm callback
});
I'd also like the dialog to show based on a data attribute on the link, rather than having to write an .on('click'... handler each time, but I don't know how to 'link' the specific confirmDialog with the function which handles the .on('click'....
This is really as far as I've got so far. I know that as I want to be able to add the functionality to any element I need to define confirmDialog() as $.fn.confirmDialog = function(){...}.
Although I can implement the entire thing in an ad-hoc way, I'm unsure as to how to implement this functionality as a clearly defined, loosely coupled reusable module.
Could someone help me get my head around how to structure this module, or provide a link to a very thorough tutorial which is specifically about writing reusable Javascript?
You can read more about how to create jQuery plugins at the following links:
http://learn.jquery.com/plugins/basic-plugin-creation/
http://www.codeproject.com/Articles/291290/How-To-Write-Plugin-in-jQuery
NetTuts videos are particularly useful:
http://net.tutsplus.com/articles/news/learn-how-to-create-a-jquery-plugin/
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I had this, and it was working and all was good.
I then tried to make the change to this, and it all went wrong.
Am I defining functions in the wrong way? Sorry, I'm new to JS & jQ (Though I'm assuming this is just basic javascript rather than anything jQuery-related)
EDIT: Also, does the MAILTO: href not work? I tried it and didn't receive an email... anyway, thanks!
You should change these:
$("#name").focusout(checkName());
to
$("#name").focusout(checkName);
You want to assign the function itself as a focusout handler, but instead you're assigning whatever the function returns as a handler.
Also, the mailto: URL scheme does not send emails. It opens whatever local mail client is defined as handler for this scheme on the visitor's machine and commences composition of a new message addressed to the address in the URL.
Pass the function name, not the result of the function executed.
$("#name").focusout(checkName);
$("#pass").focusout(checkPass);
$("#pass2").focusout(checkPass2);
Add var infront of your function names (not completely necessary but stops them being global):
var checkName = function(){
And then pass them to the event binding functions without the () as this is running the function and passing the result rather than passing the actual function itself:
$("#name").focusout(checkName);
This should fix the problem.
http://jsfiddle.net/infernalbadger/Hxnv5/6/
Also, the mailto: link works fine. It should open up your default email client.