admin管理员组

文章数量:1290949

function initiate()
{

    $("p").text("Hi", Hello());
}

function Hello()
{
    alert("Hello, you have called another function");
}

HTML:

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button onclick="initiate()">Click me</button>

Right now the script calls the function initiate() upon the button click, and then the other function, Hello() is called, while changing all paragraphs in the html to "Hi".

What I would like is that the function Initiate() will call the function Hello() using jQuery, and do nothing else.

(In other words, how do I simply call a function using jQuery?)

function initiate()
{

    $("p").text("Hi", Hello());
}

function Hello()
{
    alert("Hello, you have called another function");
}

HTML:

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button onclick="initiate()">Click me</button>

Right now the script calls the function initiate() upon the button click, and then the other function, Hello() is called, while changing all paragraphs in the html to "Hi".

What I would like is that the function Initiate() will call the function Hello() using jQuery, and do nothing else.

(In other words, how do I simply call a function using jQuery?)

Share Improve this question asked Mar 6, 2013 at 13:00 frrlodfrrlod 6,6858 gold badges33 silver badges38 bronze badges 1
  • 1 Well, how do you call a function in JavaScript? – Ja͢ck Commented Mar 6, 2013 at 13:53
Add a ment  | 

5 Answers 5

Reset to default 3

You could not pass callback function to text(). Call the hello method after changing the text.

Live Demo

Change

$("p").text("Hi", Hello());

To

$("p").text("Hi");
Hello();

I would say you don't really need jquery for this, but if you are dead set on using it, you should re-evaluate your logic here. Give your button an id first, you don't need it, but it's good to do it anyway:

<button id="btnClickMe">Click Me<button>

then, use that to trigger a jquery event on, thus calling your function:

$('#btnClickMe').click(function() { //You can either link directly to a func here
alert('Whatever'); //Or you can write the code for your func directly into the handler here
}); 

If you only want to call Hello() from Initiate(), you simply call it as follows:

function initiate()
{

    Hello();
}

What I would like is that the function Initiate() will call the function Hello() using jQuery, and do nothing else.

You don't need to call the function using jQuery, you can just call it.

function initiate()
{

    $("p").text("Hi");
    Hello();
}

The function argument taken by .text is not the second argument and neither is it for calling that function like you are trying to do. It is for setting the text of each paragraph individually by returning a value from the passed function. Since you want the text "Hi" in each paragraph, you can just use $("p").text("Hi").

This will attach an event handler that is fired when the button is clicked. You will not need the initialize function anymore nor will you need to set the text on the p. I'm having trouble discerning what you would like, however I think you may just want to call the function on a simple click of the button.

$(document).ready(function(){
  $("button").click(function(){
     Hello();
  });
});

本文标签: jQuerycalling a javascript functionStack Overflow