Solved

Whether to pass a javascript object to a function or id

Posted on 2016-10-20
3
65 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 51

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 56

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
This article discusses how to implement server side field validation and display customized error messages to the client.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

733 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