manFromMidwest
manFromMidwest

Reputation: 3

JQuery won't change text

I am very new to JQuery and I'm not sure how to better describe this problem other than that the text in the

tag won't change and this is modeled after the example on the start page of the JQuery site. The fiddle is: http://jsfiddle.net/sojwsuk1

Here is the JQuery code:

    function toggle()
    {
        $("p.greeting").html("Goodbye");
    }   

and the HTML:

    <p id="greeting">Hello</p>
    <button onclick="toggle()">Click Me</button>

Please forgive the question because I am very new to JQuery and the last time I did javascript was 3 semesters ago!

Thank you for reading.

Upvotes: 0

Views: 1716

Answers (3)

Joe Packer
Joe Packer

Reputation: 525

<p id="greeting">Hello</p>
<button id="test">Click Me</button>

$('#test').click(function(){
    $("#greeting").html("Goodbye");
})

//quick and easy way to do it without the onclick in html tag

Here is the JS Fiddle http://jsfiddle.net/sojwsuk1/7/

Also just a heads up the selector you use is looking for a class. If you are trying to select a class you use the ".", if you are looking for ID you use the "#".

Upvotes: 0

Exception
Exception

Reputation: 8389

Your toggle method should be placed in head, in fiddle that is loaded after html. So use like below to make it in other ways way

$('button').on('click', function(){
   $('#greeting').html('GoodBye');
});

and also check bind (if jQuery < 1.7.2 ) and delegated events with on method

Check this http://jsfiddle.net/sojwsuk1/5/

Upvotes: 0

feng smith
feng smith

Reputation: 1537

greeting is the id of the p tag, you can use:

$("#greeting").html("Goodbye");

You can learn jquery through this site: http://www.w3schools.com/jquery/default.asp, the selector of jquery is very likely the selector of css.

Upvotes: 2

Related Questions