Solved

Whether to pass a javascript object to a function or id

Posted on 2016-10-20
3
71 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 52

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 57

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
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.

696 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