mercredi 25 février 2015

Can you define another string by the text from another string in jquery?

guys. I was wondering if it's possible to define a string by using an "identifier" string with the text of the string that has to be changed?


I have some code below, where you can see an example.



var x1 = 1;
$('.shhi').click(function(){
var data_id = $(this).attr("data-id");

var container_class;
var identifier;
if(data_id == 1){
container_class = "information_container";
identifier = "x1";
}

if(identifier == 0){
$(this).html('<i id="e_' + data_id + '" class="showhide fa fa-plus-circle"></i> Skjul');
$('#' + container_class).show();
identifier = 1;
$('#e_' + data_id).removeClass("fa-plus-circle").addClass("fa-minus-circle");
}else{
$(this).html('<i id="e_' + data_id + '" class="showhide fa fa-minus-circle"></i> Vis');
$('#' + container_class).hide();
identifier = 0;
$('#e_' + data_id).removeClass("fa-minus-circle").addClass("fa-plus-circle");
}

});


So as you can see, I define the x1 string, and then I tell the identifier string that it's the x1 string that has to have a value changed, when it's clicked. But the problem here, is that it doesn't change the "x1" string, probably because it changes the identifier string instead.


Does anyone know a way to get around this? Or would I have to make something that checks the identifier, and then change the x1 etc. out from that? But I'd like it to be clean.


Aucun commentaire:

Enregistrer un commentaire