0

So in my code I used

function $(id) {
    return document.getElementById(id);
}

I want to move to jQuery. How to translate such thing to it?

4 Answers 4

3

Don't use this function, function $(id). Use the jQuery function $('#id'). This function will return an object with bunch of jQuery methods.

Methods include remove(), hide(), toggle(), etc., and the implementation is like $('#id').hide(), $('#id').show(), etc.

There are so many, many jQuery methods, that simplifies it in so many ways.

Sign up to request clarification or add additional context in comments.

Comments

1

If I got your question right, you will need to include jQuery inside your page, and replace the following line:

return document.getElementById(id);

with this one:

return $("#"+id);

2 Comments

Oh no, don't make another $ function, that's completely unneeded.
I'm not doing so, just answering his question. and yes, I agree that he shouldn't use a function called $(id), but I didn't see his full code to assume that he can't use such function.
1

Using Jquery selectors you can do the same thing very easily.

$("#" + id)

For more details:

http://api.jquery.com/id-selector/

Comments

1

Just use

$("#"+id)

That will return the element with the right ID.

Comments

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.