1

I'm just trying to add some string or HTML tag inside div #OL_Icon_63 using jQuery .append(), here's the example of code :

$(document).ready(function() {
    $('#OL_Icon_63').append('HELLO WORLD');
});

But that's code won't work for me, I try to change the code like this :

$(document).ready(function() {
    $("body").click(function() {
       $('#OL_Icon_63').append('HELLO WORLD');
    });
});

And

$(document).ready(function() {
    $("body").hover(function() {
       $('#OL_Icon_63').append('HELLO WORLD');
    });
});

Two kinds of code work for me where I use .hover() and .click() after body tag, but i basically can not use the function like .append() or anything else without adding .hover() or .click() after $("body") but when I use click or hover the string appeared repeatedly

Can anyone give me some example / opinion so that .append() jQuery function will work perfect for me?

3 Answers 3

1

A more general solution is using the fact javascript functions are objects, once the function clickAction is called it will set itself to a blank function after appending the text

EDIT This does not work in jQuery due to the way it handles events, however it does contain a method called one()

$(document).ready(function() {
  var clickAction = function(){
     $('div').append("Hello World");
  } 
    $("div").one("click",clickAction);
});​
Sign up to request clarification or add additional context in comments.

4 Comments

I basically can not use the function without adding .hover() , .click() after $("body") but when I use click or hover the string appeared repeatedly
i just try to change div to #OL_Icon_63 , but it doesn't work for me Any another way, not use .click() or .hover(), so the string can open automatically when i'm not click or hover it ?
Oh sorry, i think the bottom div on .one() must be change to body :D And your help is realy helped for me, so thank you for your help, i'm so very grateful :)
i have a more question againt, i'm just edit that code, but the hover function wouldn't work, is there are error or the code syntax is wrong? You can view here : pastebin.com/QpESh0C4 . Thank you
0

Use text instead:

$('#OL_Icon_63').text("hello world")

You are using append incorrectly. Append is for adding elements to the innerHTML of an element. text is for text, and html is for HTML

This will also replace all text in the element, so there will be no multiples.

1 Comment

I basically can not use the function without adding .hover() , .click() after $("body") but when I use click or hover the string appeared repeatedly
0

why don't you try without $(document).ready this works for me:

(function(){
    $('#OL_Icon_63').append('HELLO WORLD');
})();

Working example: http://jsfiddle.net/tgzKQ/

3 Comments

Sorry, but that's code is still not working for me :) but i'm so very thanksfull about your answer And about the downvote, i'm not give the downvote, but who the person so very cruel like that
Can you add more of your code to the example so we can see what else it's messing up your code?
I'm so very grateful you will be give me the answer if the code is mess or got some errors :) but the apps is still current inside my localhost :( but i will be upload it soon.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.