Solved

Whether to pass a javascript object to a function or id

Posted on 2016-10-20
3
46 Views
Last Modified: 2016-10-25
HI,
I am making a search filter...
And how i am intending it to give it to user... is ...like :
user will provide the following html in its file :
<div id="filter"/>
And then in js will invoke my library with the parameter filter
And my code will replace this div with the search filter html and everything else....

Now there is an  alternative approach to this :
User can create a $('#filter') and pass it to my function or any other javascript object DOM Node etc...

Which approach is better passing the id as a string or passing an object... and which one....

Also the search filter html which i will return should append to this div element or replace this element ?

Thanks
0
Comment
Question by:Rohit Bajaj
3 Comments
 
LVL 7

Assisted Solution

by:Thomas Wheeler
Thomas Wheeler earned 125 total points
ID: 41852679
I would use the ID rather than the object that way you know what it is you are getting.
0
 
LVL 49

Assisted Solution

by:Ryan Chong
Ryan Chong earned 125 total points
ID: 41853172
in my opinion, it's depending on the tasks you're doing in your function. There is no absolute answer for right or wrong to either passing an ID or the object itself to the function since we can do handling in both ways.

it's also a bit of engineer's behavior to determine which approach is preferred, one my feel it's easy and more controllable in one way but another person could have a different opinion.
0
 
LVL 52

Accepted Solution

by:
Julian Hansen earned 250 total points
ID: 41853373
Normal plugin behaviour is

$('.selector').filter()

Open in new window

However, as you are replacing the element another option would simply be
<div class="my-unique-filter-class-name"></div>

Open in new window


When your library loads
$('.my-unique-filter-class-name').each(function() {
   // replace with your widget here
});

Open in new window


User does not have to do anything other than add the right class to their element.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Trouble inserting a variable  in javascript 7 42
.php tree directory? 5 54
Regular Expression Calculator Tester 2 47
compact pure CSS Read More Toggle 4 18
Read about why website design really matters in today's demanding market.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now