Try with following code:
<script>
$(document).ready(function(){
$("[id^=tolevel_]").click(function(){
var currentID = $(this).attr("id");
var number = currentID.replace("tolevel_","");//get the number in string format
number = parseInt(number);
var i = number -1;
$("#level_" + i).hide(500,'swing', function(){
$("#level_" +(i+1)).show(500, 'swing', function(){
});
});
});
$("[id^=backtolevel_]").click(function(){
var currentID = $(this).attr("id");
var number = currentID.replace("backtolevel_","");//get the number in string format
number = parseInt(number);
var i = number + 1;
$("#level_" + i).hide(500,'swing', function(){
$("#level_" +(i-1)).show(500, 'swing', function(){
});
});
});
});
</script>
It user `jquery’s Attribute Starts With Selector. See more about it at https://api.jquery.com/attribute-starts-with-selector/. Really no need to write code for each and every element. This should work.
1
solved why my jquery code is not working?