Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9014949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:37:51+00:00 2026-06-16T03:37:51+00:00

I am using Ruby on Rails, jQuery v1.8.3 and jQuery UI v1.9.2. In a

  • 0

I am using Ruby on Rails, jQuery v1.8.3 and jQuery UI v1.9.2. In a view file I am rendering a partial template this way:

<%= render :partial => 'template_name', :locals => { :div_id => 'div_id1' } %>

The related template_name file contains the following:

<script type="text/javascript">
  var item = $('#<%= div_id %>')

  alert(item.selector) // Display #div_id1
</script>

On the same page, through an AJAX request, I retrieve and add to the DOM the same partial template but with :locals => { :div_id => 'div_id2' }:

<script type="text/javascript">
  var item = $('#<%= div_id %>')

  alert(item.selector) // Display #div_id2
</script>

Making the above, after the AJAX request, I “lose” the reference to the #div_id1 since item becomes related to #div_id2.

How should I handle that situation in order to avoid “overwriting” variables? How can I “isolate” (maybe, namespacing or using some jQuery features) those variable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:37:53+00:00Added an answer on June 16, 2026 at 3:37 am

    If the code snippets are self-contained, you should be able to just move the code into a private scope.

    <script type="text/javascript">
        (function(){
            var item = $('#<%= div_id %>');
            alert(item.selector); // Display #div_id2
        })();
    </script>
    

    Though i disagree with having server-side code generating javascript-based ui widgets.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using jQuery File Upload with Ruby on Rails 3 $(.jquery-upload).fileupload({ url: /photos, type:
I'm using ruby on rails + jquery ajax My function is for user check
I am using Ruby on Rails, but I think this question could be applied
I am using Ruby on Rails 3 and I would like to use jQuery
I'm using Ruby on Rails with jQuery and trying to do the following: <%=
I have a Rails application and I'm using jQuery to query my search view
I am trying to make a auto complete box using ruby on rails, jquery
I'm using the jQuery FullCalendar plugin with Ruby on Rails, and now I want
I am using Ruby on Rails 3.0.7 and jQuery 1.6.1. I would like to
I've installed RoR in my MacOS X using this guide http://ryanbigg.com/2010/12/ubuntu-ruby-rvm-rails-and-you/ Here my versions:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.