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

  • SEARCH
  • Home
  • 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 8887187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:44:42+00:00 2026-06-14T21:44:42+00:00

How to shift between variable names in jQuery and change them? //predefined variables var

  • 0

How to shift between variable names in jQuery and change them?

//predefined variables
var s1='';
var d2='';
//trying to change variables by .attr() parameter but no luck
$('body').on('click','span', function() {
var $(this).parent().attr('data-scan')=$(this).attr('id');
});

HTML

<div data-scan="s1"><span id="banana">Banana</span><span id="apple">Apple</span></div>
<div data-scan="d2"><span id="orange">Orange</span><span id="apple">Apple</span></div>

How can I change specific variables? I do’t care about changing attr papameter, all I need is changing predefined global var parameters!

  • 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-14T21:44:43+00:00Added an answer on June 14, 2026 at 9:44 pm

    Firstly, your html for the data-scan attributes is wrong, you have no closing quotes.

    Secondly, you can the data() jquery function to access data attributes.

    Thirdly, you cannot set values by using the = operator.

    You want something like this:

     $(this).parent().data('scan', $(this).attr('id'));
    

    or, without the data() function:

     $(this).parent().attr('data-scan', $(this).attr('id'));
    

    Here is a working example


    To get the value you can do one of the following:

    var dataScan = $(this).parent().data('scan');
    

    or

    var dataScan = $(this).parent().attr('data-scan');
    

    Your exact requirements for setting a variable based on the data-scan value

    Based on your comments and code, I think it has not been clear what you were trying to do. I think I have worked it out though and you want to use the data-scan value to determine which global variable should be set…

    //predefined variables
    var s1='';
    var d2='';
    
    $('body').on('click','span', function() {
        var variableType = $(this).parent().data('scan');
        var valueToSet = $(this).attr('id');
        if(variableType == "s1"){
           s1 = valueToSet;
        }
        else if(variableType == "d2"){
           d2 = valueToSet;
        }
    });
    

    Here is an example of what I think you are trying to do.

    However, if you have lots of variables then it is not ideal to use so many if/else statements. So you could use the javascript eval() function.

    var variableType = $(this).parent().data('scan');
    var valueToSet = $(this).attr('id');
    
    eval("" + variableType + " = '" + valueToSet + "';");
    

    See here for an example

    But be careful the your eval code is subjected to user injected values (not that javascript is safe from users anyway)

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

Sidebar

Related Questions

I know that F7/Shift F7 will switch between Code behind and ASPX pages, but
(C code) how would I pass my global variables between functions and return them
I declared a global shift operator but for some reason the compiler cannot deduct
Im trying to shift my array left as efficiently as possible. Im using pointers
I'm trying to get a feel for the difference in performance between integer multiplication
What is the difference between Ctrl + Shift + R and Ctrl + Shift
I use the Shift + F7 often to switch between source and design view.
What is the difference between Percolate-down/Shift-down and Heapify operation? This is my Shift-down function
Is it possible to pass variables between multiple calls to the around MethodModier ?
I know there are browser rendering inconsistencies between Chrome and Firefox (Windows, both). But

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.