Hide div having variable dynamic ID - javascript

The div is a switch which gets a dynamic ID .
class name is impbtn , id is generated in variable this.impbtn6.id
HTML:
<div id="widget-id63032Candy_Eaten_importGoodsBtn" class="impbtn"></div>
There are two places hide is required - click and onload
In click event
document.getElementById(this.impbtn6.id).style.visibility="hidden";
works fine .
I cannot use Document.getelementbyID , as multiple form loads occur and button is not supposed to be hidden then .
So i trued using JQ to access css properties
This
jQuery('.impbtn, #this.impbtn6.id').css('visibility',"hidden");
works but makes all button in the class invisible . I want to make only this.expbtn6.id invisible not all ids under this class .
I have read each and every page available.Some things Iv unsuccessfully tried (Separately)
var vid= this.impbtn6.id;
jQuery("#"+ vid).visibility("hidden");
$('#vid .impbtn').css('visibility',"hidden")
var row2=$(".impbtn").find("div#"+vid);
row2.hide();
$('#vid .impbtn').css('visibility',"hidden");
$('div#vid').css('visibility',"hidden");
$('.impbtn', $("#div" + this.impbtn6.id)).css('visibility',"hidden");
$("#div"+ vid).css('visibility',"hidden");
$("#"+ vid).hide();
$('#vid').css('visibility',"hidden");
row = $('#' + vid);
row.css('visibility',"hidden");
I would highly appreciate a reply/comment.

your question is a little confusing as you say
document.getElementById(this.exportbtn6.id).style.visibility="visible";
works fine but this code shows the div, not hides it, and why cannot the same code be used to hide if you use it to show?
then your other snippet that supposedly works:
jQuery('.expbtn, #this.exportbtn6.id').css('visibility',"visible");
cannot work because #this.exportbtn6.id will not resolve to the variable's content inside the double quotes, so i am pretty sure this line will not do anything.
The way to do this correctly is
jQuery('#' + this.exportbtn6.id + '.expbtn').hide();
but i cannot tell for sure as the question is not clear. If my understanding of your question is correct, the above line will do the trick. Keep in mind that the value of "this" will differ based on context, so it maybe that you are referencing the wrong "this".

Try this
$('#this.exportbtn6.id').css('visibility','visible');

With jQuery:
$("#"+this.expbtn6.id).hide();
I hope it helps.

Related

Strange behaviour with jquery load function

I am using jquery's load function to updated a div inside my jsp. However the issue is, even if I give the wrong div id the correct div gets updated but if I give the correct div id , it doesn't work.
Inside my jsp:
<div id="markers" class="height-400"></div>
Inside Js:
$("#marker").load(location.href + " #marker>*", ""); //works (notice marker instead of markers)
$("#markers").load(location.href + " #markers>*", ""); // Doesn't work
Can someone please help me understand what's going on here.
Referring to this post Refresh/reload the content in Div using jquery/ajax
Try using the code without the double quotes after your CLASS/ID.
$("#markers").load(location.href + " #markers>*");

Access html elements created by js in other js functions

I am trying to access html elements that I create in one js function in another function. I have this code
EDIT after comments:
here is a jsfiddle
http://jsfiddle.net/8uTxM/
</button>" +"<button value='1' type='button' class='as' id='c2' onclick='cA(this);'>"
in this function
function cA (element){
var x = element.value;
if (x === allQuestions[questionNumber].correctAnswer) {
element.setAttribute.style.backgroundColor = "green";
++score;
}
}
I am trying to make the button green when it is clicked. However, I get the error:
Cannot set property 'backgroundColor' of undefined
I assume this has something to do with timing, but I cannot figure out why. Especially since the element.value bit works (the++score works fine, every right question adds +1 to the score variable)
One problem that I may guess is you are using "getElementsById"
either go for "getElementById" or "getElementsByTagName"
Why don't you create a <div> in your html/php page which would be empty with the answers class, and then change its id/innerHTML ?

jquery body on hover cannot target other DIVS

I am trying to access my own javascript function from within a:
$("body").on("mouseenter",".noteNode",function(){});
The trouble seems to be that when I am inside the function above jQuery will not allow me to get access to other elements?
Let me elaborate further:
Actual code I want to use I've jsfiddled: http://jsfiddle.net/uJ2Yb/8/
I believe the function is being called as I had wanted it to be, but as you can see $(this) is not able to target the actual div which is being hovered, and so when trying to pass this data to the other function it just doesn't arrive - why is this? I believe it's a scope problem but I have tried a few ways to solve it and searched S.O without much luck yet.
The reason I need to use $("body").on is because I am creating the DIV's (which will be hovered) on the fly with JS.
As you can see the hover function is working fine and firing as it should do, but totally unable to access anything in the DOM which I don't understand
.
[Edit] so to clarify: http://jsfiddle.net/uJ2Yb/8/
I am trying to pass the ID of the DIV (which was created BY JS) over to my own function in which i want to try and access the DIVs offset parameter - but i am unable to do so, you will see in my revised fiddle that i am just getting 'undefined' in my alert box which is not what i wanted - i am still convinced that this is because of a scope issue.
try this updated fiddle: http://jsfiddle.net/uJ2Yb/7/
function myFunction(someVar){ alert(someVar); }
$("body").on("mouseenter",".noteNode",function(event){
myFunction(event.target.innerHTML );
//alert( event.target.innerHTML );
});
Please Assign Unique Id To Your Divs
try This Code
function myFunction(someVar){ alert(someVar); }
$(document).ready(function(){
$("body").append('<div class="noteNode" id="a">Note Node</div>');
$("body").append('<div class="noteNode" id="b">Note Node</div>');
$("body").append('<div class="noteNode" id="c">Note Node</div>');
});
$("body").on("mouseenter",".noteNode",function(){
myFunction( $(this).attr("id") );
alert( $(this).attr("id") );
});

Which event i must use?

I have this code :
htmlDiv+="<DIV class='addon-checkbox'
onclick='event.stopPropagation();__toogleEnabled(\""+__addons[i]+"\", this);'
if (localStorage[\"__Enable__\"+__addons[i]]==\"yes\")
this.style.backgroundPosition= \"0 -50px\"; >";
and when click on this div function __toogleEnabled correctly change background-position of image, but rest code do nothing.
I guess that this(receive current element) or whole instruction
if(localStorage[\"__Enable__\"+__addons[i]]==\"yes\")
this.style.backgroundPosition= \"0 -50px\"; >";
dont work because there is no event. But i dont know what event must be.
May be it can be fix with another solution. I just want that background-position have value "0 -50px" , if localStorage[\"__Enable__\"+__addons[i]] have value "yes" when page loaded , but it must be done only for current div because there are many other div with same class and without id.
I think you're getting your quotes mixed up. Your code needs to be changed to:
htmlDiv+='<div class="addon-checkbox" '+
'onclick="event.stopPropagation();__toogleEnabled(\''+__addons[i]+'\', this);'+
'if(localStorage[\'__Enable__\'+__addons[i]]==\'yes\')'+
'this.style.backgroundPosition=\'0 -50px\';" >';
But a much better way to do all this would be to use the DOM.

i am trying to get value of particular hidden input text box using jquery it's not working

i tried before insert this
var req=$('#'+investor_id+'#requestId');
var requestId=document.getElementById(req).value;
alert(requestId);)
code it's it's working fine but after it's not working, here is code
You should change your code a bit: http://jsfiddle.net/rkw79/9fTPh/17/
That fiddle cleaned up your code slightly, but you should normally take this steps:
1) hit the 'tidyup' button, then look for missing });
2) don't use id's if they're not unique, just use class
3) you do not have to set an id on everything just to find it, all objects are relative to each other
Why don't you access element with its id instead of writing pipings. ID values are unique in document.
Try this code
alert($('#requestId').val());
//Check here for working demo http://jsfiddle.net/9fTPh/6/
Instead of
var req=$('#'+investor_id+'#requestId');
var requestId=document.getElementById(req).value;
alert(requestId);
And in the fiddle you have provided code us },) are not correctly given.(Check with JsLint)

Categories

Resources