0
 function bookRemoved(bookId)
     {
         alert(bookId) ;
         $(document).ready(function() {
         $('.className').hide(1000); // Want it Here

      } );}

In Above example, the bookId returns class Name. How can I use it as class name to hide it as shown in above example.

I would like something like this:

     $('.bookId').hide(1000); // Want it Here

bookId should return its value. Suppose if bookId has 23, it should look like this. $('.23').hide(1000); // Want it Here

PS: I am new to Javascript and Jquery

1
  • Btw, class names aren't supposed to start with a number. Commented Aug 12, 2013 at 5:27

5 Answers 5

4

Try like

function bookRemoved(bookId)
{
     alert(bookId) ;
     $('.' + bookId).hide(1000); // Want it Here

}

We dont need DOM ready there,in the defined function.

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

Comments

1

What you are looking for is a means of passing the argument of one function as argument to another. Variables can be passed into functions, $ is just a function, within the (...) sequence following a function reference.

function bookRemoved(bookId) {
    alert(bookId);
    $(document).ready(function() {
        $('.'+bookId).hide(1000);
    });
};

In this case:

  • the function alert is fired on the variable bookId
  • the function $ is fired on the document variable.
  • the method ready is accessed from the returned object of $(document)
  • ready is fired with a function, an anonymous function (i.e., without a name)
  • the function $ is fired on a string concatenation between . and bookId
  • the hide method is then accessed and fired with 1000

Hopefully this better explains the general terminology and what it is that is occurring within the function definition.

Comments

0
function bookRemoved(bookId) {
    alert(bookId);
    $(document).ready(function () {
        $('.' + bookId).hide(1000);
    });
}

Do you mean this?

Comments

0
$('.' + bookId).hide(1000);

In javascript + operator is used to...

  1. add nembers.
  2. concatenate strings if one of the operand is string.

for example...

1 + 2 == 3

"abc" + "xyz" == "abcxyz" and

"1" + 2 == "12" // "1" here is a string

Comments

0

Cleaner approach. Seperate your document.ready function.

$(document).ready(function() {  

    function bookRemoved(bookId) {
        $(bookId).hide(1000);  
    }

    //pass in your specified class name OR id as the argument.
    bookRemoved('.someClasssName'); 

});

More info on hide method here: http://api.jquery.com/hide/

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.