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 3691050
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:06:14+00:00 2026-05-19T04:06:14+00:00

I’m trying to pass a variable from one function to another, but the var

  • 0

I’m trying to pass a variable from one function to another, but the var elmId is being returned as an object and giving an error. When we click on any of the generated divs we should be able to change the size of the div by choosing a width / height value from the drop down menus.

I’m trying to pass the clicked div id which is elmId to function displayVals but it is not working. If we replace “#”+elmId in the function displayVals with the actual id of the first div created with is “#divid1” then it works. Why is the value of var elmId not being passed to displayVals

<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/themes/base/jquery-ui.css" type="text/css" media="all" /> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js" type="text/javascript"></script>
   <style>
    .aaa{width:100px; height:100px; background-color:#ccc;}
    button{width:100px; height:20px;}    
    </style>

<button class="idiv">div</button>
<select id="width">
    <option>100px</option>
    <option>200px</option>
    <option>300px</option>
</select>
<select id="height">
    <option>100px</option>
    <option>200px</option>
    <option>300px</option>
</select>
<p></p>

<script type="text/javascript">
var divId = 1;

$("button").click(function(){
        var elm = $('<div id=divid' + divId + ' class=aaa></div>');
        elm.appendTo('p');
        divId++;
});

$("p").click(function(e){
    var elmType = $(e.target)[0].nodeName,
    elmId = $(e.target)[0].id;
    return displayVals(elmId);
    });
function displayVals(elmId) {
    var iwidth = $("#width").val();
    var iheight = $("#height").val();
    $("#"+elmId).css({width:iwidth, height:iheight});
    console.log(elmId);
    }

    $("select").change(displayVals);
    displayVals();
</script>
  • 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-05-19T04:06:15+00:00Added an answer on May 19, 2026 at 4:06 am

    CSS:

    .divs{width:100px; height:100px; background-color:#ccc;}
    button{width:100px; height:20px;}    
    .selected{background:#dd0000;}
    

    jQuery:

    <script type="text/javascript">
    var divId=1,selecteddiv="";
    $("button").click(function(){
        $('p').append('<div id="divid'+divId+'" class="divs"></div>');
        divId++;
    });
    
    $(".divs").live("click",function(){
        selecteddiv=$(this).attr('id');
        $(this).addClass('selected');
        return false;
    });
    
    function displayVals() {
        if(selecteddiv!=""){
            var iwidth = $("#width").val();
            var iheight = $("#height").val();
            $("#"+selecteddiv).css({width:iwidth, height:iheight});
        }
    }
    
    $("select").change(displayVals);
    displayVals();
    </script>
    
    • button will create the divid[X]
    • as you’re generating them dynamically you need to you live (as they don’t exist when click event is being attached to class .divs)
    • click on one of the .divs will put the ID name inside selecteddiv
    • just to show you which one is selected the .selected class will be added to this div
    • when you’ll change the value .change event will kick in and if selecteddiv isn’t empty then it will change the width and height
    • you can drop the last line displayVals(); as you don’t have any divs created at the begining

    I hope that’s helpful enough 🙂

    Cheers

    G.

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

Sidebar

Related Questions

No related questions found

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.