maddogandnoriko
maddogandnoriko

Reputation: 1002

jQuery cached element

I have a function that uses a cached element(el) to change the font size of 2 divs. When the function executes I need to make sure the font is not too small or too large based on which div it is(tagCommon or tagLatin). So how can I determine in the function which element was passed to it via el?

I think I may be over thinking this or doing it wrong, it kinda feels like I am hacking it in...and usually when it feels like that there is something wrong.

var cb               = $('#tagCommon');
var lb               = $('#tagLatin'); 
changeFontSize(cb,1,'up');

function changeFontSize(el,amount,UporDown){
   var size = parseFloat($(el).css("font-size").replace(/px/, ""));
   // do some stuff here

   // ????????
   if(el == $('#tagCommon')) //check font size and alert if too small
   if(el == $('#tagLatin')) //check font size and alert if too small
}

Thank you for your time.

Todd

Upvotes: 2

Views: 162

Answers (2)

thecodeparadox
thecodeparadox

Reputation: 87073

function changeFontSize(el,amount,UporDown){
   var size = parseFloat($(el).css("font-size").replace(/px/, "")),
       id = el.attr('id'); // or el[0].id

   if(id == 'tagCommon') //check font size and alert if too small
   if(id == 'tagLatin') //check font size and alert if too small

   // OR
   if(id == 'tagCommon')
   if(id == 'tagLatin')

   // OR
   if(el.is('#tagCommon'))
   if(el.is('#tagLatin'))
}

.attr('id') will retrieve the id and match with supplied one

.is() matched set of elements against a selector, element, or jQuery object. Return value boolean true/false.

Upvotes: 1

Mohammed Swillam
Mohammed Swillam

Reputation: 9242

use jQuery is() method

Check the current matched set of elements against a selector, element, or jQuery object and return true if at least one of these elements matches the given arguments.

if(el.is('#tagCommon'))
    {
      //  your code here
    }

Upvotes: 3

Related Questions