0

I'm trying to allow a user to enter in a number (i.e. 3 in the first div below) that will decide which div's to show in an output. I created an array of id's (or so I thought) based on the number entered. When I alert the array the id's look correct but for some reason when I try and show them in the function below nothing happens. This is very similar to this post: how to loop through an array of jquery objects and .hide() each of them but I can't re-create it. Any help would be greatly appreciated. Here's a fiddle: http://jsfiddle.net/dbeau79/4KzuN/

<div id="variable_1">3</div>

<div id="jinja_1" class="jinja">I'm Jinja 1</div>
<div id="jinja_2" class="jinja">I'm Jinja 2</div>
<div id="jinja_3" class="jinja">I'm Jinja 3</div>
<div id="jinja_4" class="jinja">I'm Jinja 4</div>
<div id="jinja_5" class="jinja">I'm Jinja 5</div>
<div id="jinja_6" class="jinja">I'm Jinja 6</div>    

$('.jinja').hide();    
var varStart = 1;
var varEnd = $('#variable_1').text();

var arr = [];

while (varStart <= varEnd) {
arr.push("'#jinja_" + varStart++ + "'");
}

$.each(arr, function () {
//alert(this);
$(this).show();
});

4 Answers 4

0

this would solve your problem:

<div id="variable_1">3</div>

<div id="jinja_1" class="jinja">I'm Jinja 1</div>
<div id="jinja_2" class="jinja">I'm Jinja 2</div>
<div id="jinja_3" class="jinja">I'm Jinja 3</div>
<div id="jinja_4" class="jinja">I'm Jinja 4</div>
<div id="jinja_5" class="jinja">I'm Jinja 5</div>
<div id="jinja_6" class="jinja">I'm Jinja 6</div>    

//$('.jinja').hide();    
var varStart = 1;
var varEnd = $('#variable_1').text();

var arr = [];

while (varStart <= varEnd) {
arr.push("#jinja_" + varStart++ + "");
}

$.each(arr, function (i, elem) {
//alert(this);
$(elem).show();
});
Sign up to request clarification or add additional context in comments.

Comments

0

I would use a completely different approach: http://jsfiddle.net/4KzuN/13/

<input type="number" value="3">

<ul>
    <li>I'm jinja 1</li>
    <li>I'm jinja 2</li>
    <li>I'm jinja 3</li>
    <li>I'm jinja 4</li>
    <li>I'm jinja 5</li>
</ul>

<script>
    $('input').on('keyup', function() {
        $('li').hide();
        $('li').eq(parseInt($(this).val(), 10) - 1).show();
    }).keyup();
</script>

1 Comment

I should've been more clear but what I needed was to show all the elements that were in the array not just the last one. I like this code though and could use it as well. Thank you for your response.
0

Hope you want this:

var id = $('#variable_1').text();

$('[id*=jinja_]').hide();

// Step One
$('#jinja_' + id).show(function(){
    $(this).prevAll('div').show();
});

// Step Two
$('[id*=jinja_]').hide();
$('#jinja_' + id).show();
$('#jinja_' + id).prevAll('div').show();

Depends on you which one you like to use.

JSFIDDLE

1 Comment

very tidy! This is a huge help as well. Thank you.
0

Just a different approach - FIDDLE - not better than another other. Perhaps limited in older browsers.

CSS

.jinja {
    display: none;
}

JS

var mynumber = +$('#variable_1').html() + 1;

$('.jinja:nth-child('+ mynumber +')').css('display', 'block');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.