Solved

Whether to pass a javascript object to a function or id

Posted on 2016-10-20
3
33 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 6

Assisted Solution

by:Thomas Wheeler
Thomas Wheeler earned 125 total points
Comment Utility
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
Comment Utility
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 51

Accepted Solution

by:
Julian Hansen earned 250 total points
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

771 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

10 Experts available now in Live!

Get 1:1 Help Now