Insert after an element - logic help
I have the following:
$.post(baseUrl+"/index/voto",{id:eid},function(e){
var divMsg = $("#msg");
var html = "";
if(e.msg == 1){
html = '<img src="'+baseUrl+'/lib/img/showImageA.png" alt="pdf" />';
}else if(e.msg == 3){
//HERE I MUST DISPLAY A TEXT AFTER A GIVEN ELEMENT
开发者_如何学Python } else if(e.msg == 2){
html = '<img src="'+baseUrl+'/lib/img/showImageB.png" alt="pdf" />';
} else{
html = '<img src="'+baseUrl+'/lib/img/showImageC.png" alt="pdf" />';
}
divMsg.show();
divMsg.html(html);
}, 'json');
So, if something arrives, I will replace the content with an image. This works.
The issue is that, on the second case (e.msg ==3)
I should not remove or replace the all thing with an image, but I need to append an element p on a specific place saying: "something".
I tried to display:none; that element and then add:
else if(e.msg == 3){
myParagrafId.show();
}
Nothing appears.
I tried to:
else if(e.msg == 3){
$('#foo').append('<p>Something</p>');
}
Still nothing...
The function seems to be returning the html, ignoring my instructions inside the second conditional.
So, perhaps I need to grab the existing html part, append what I need, and then allow the function to output it ?
Additional note: If I place an alert(); inside that condition (e.msg == 3) it works.
First af all you are always putting html
into $("#msg")
here
//in case 3 html = ''
divMsg.html(html);
so you should check it's not an empty string:
if (html != ''){
divMsg.html(html);
}
So if you were trying to do somenthing inside $("#msg")
(like showing an hidden div inside it) in case 3 it does nothing because it gets replaced by '' because you are calling $("#msg").html('');
does this help?
Otherwise you are doing something wrong in that if, if you show your markup we can help
精彩评论