How can I enable a fieldset? [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Normal disclaimer that I'm a terrible web developer.
I was lead to believe that, with a <fieldset disabled id="homeFieldset">...</fieldset>, I could enable its controls with $("homeFieldset").disabled = false, but alas...nothing. Also, I'm testing this in Chrome, where it should work. Fiddle: https://jsfiddle.net/ggrxx4hm/1/
FWIW, debugging shows me that $("#homeFieldset").disabled is undefined. However, if I do $("#homeFieldset")[0].disabled = false, it works. Fiddle: https://jsfiddle.net/7edje5vk/
Edited selector in second paragraph.
Edit again: But why doesn't .disabled = false work?
Answer to that via #mplungjan in the comments below: "jQuery objects do not have a disabled attribute. DOM objects do. $("#homeFieldset").disabled=false is not valid jQuery, document.getElementById("homeFieldset").disabled=false; is valid DOM manipulation"

JS Fiddle
$("#homeFieldset").prop('disabled', false);

It will work in any of these ways.
$("#homeFieldset")[0].disabled = false; // accessing the DOM object
OR
$("#homeFieldset").attr('disabled',false); // using the jQuery attr
Your code doesnt work since you have to tell jquery to change Attribute.

for enable
$("#homeFieldset").prop('disabled', false);
and for disable
$("#homeFieldset").prop('disabled', true);

Your jQuery selector is wrong. Use $("#homeFieldset") - note the # in the selector.
Read more on jQuery selectors here: jQuery API

Related

Get the height of a div using Javascript [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 months ago.
Improve this question
When I type
console.log(document.getElementById('kitHeight').offsetHeight);
It prints 30 on the console, which is wrong.
But, when I type the same using developer console, it gives me 65, this is correct.
The height of the div is 65px.
Kindly help me why this behaves like it does and how to get 65px as the correct answer using javascript
#CBroe already told you that the information you are offering is too little to help you.
But it might be the case, that you are trying to run the script before the DOM actually rendered the div. See this example:
<script>
console.log(document.getElementById("class").offsetHeight);
//Errors
</script>
<div id="class"></div>
<script>
console.log(document.getElementById("class").offsetHeight);
//Returns: 0
</script>
<script>
const i = document.getElementById("class");
i.innerHTML = "John Doe";
console.log(document.getElementById("class").offsetHeight);
//Return: 19
</script>
The second and third pieces of code will return the height. The first one will error because the div is not in the DOM yet. The third one will return an updated size from dynamic content.

on change event not working [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
1) The code(Change Event) that is not working is:
$(document).on('change','select[name="id"]',function(){
alert('Not Working');
});
2) The code(Change Event) that is working is:
$(document).on('change',$('select[name="id"]'),function(){
alert('Working');
// Here $(this).val() not working for get value.I want to use $('select[name="id"]').val()
});
Doubts:
a) Why Is First change event not working?
b) Second change Event is Working, But $(this).val() is not working.
Can you please explain the difference between above 2 functions?
In order to handle select element change:
$('select[name="id"]').on('change',function(){
alert($(this).val());
});
For dynamically created elements use:
$(document).on('change','select[name="id"]',function(e){
alert($(e.target).val());
});
As you see, in that case e.target.val() should be used ( e.target wrapped into $() in order to get it as a jquery object )
I'm not sure why your first example doesn't work as I think it should, however the second example will have this scoped to document instead of the select.
The following will scope this to the select.
$('select[name="id"]').on('change', function (){
$(this).val()
});

Get parent div id of second javascript? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a div which is like this:
<div id="mainDiv">
<script>
javascript here...
</script>
</div>
I basically want to get the ID of the div it is inside.
I have tried using jQuery and classic javascript to do this but it keeps returning undefined.
Does anyone have any idea's? Thanks
Since broswer read the code from up to down, you can do this:
Vanilla JS
var scriptTags = document.getElementsByTagName('script');
var id = scriptTags[scriptTags.length - 1].parentNode.id;
jQuery
var id = $('script').last().parent().prop('id');
when it will read this code, the last script tag is the one the browser is reading.
Here's a fiddle : http://jsfiddle.net/nb234/
Check this out http://jsfiddle.net/78N9A/1/
<div id="parent">
<script id="scr">
function show()
{
alert('hello');
}
</script>
</div>
window.onload = function(){
var ele = document.getElementById('scr').parentNode;
alert(ele.id);
}
<script id="script">
Then
$('#script').parent();
If I understand correctly, you are saying that the id on the parent div is unknown in advance, and you want to use JS to determine what it is.
If you can't edit the script tag to add an id, maybe you could do a document.write() within that block to add in a little hidden element, then you can test to see what that element's parent is.
http://jsfiddle.net/AtRYF/
JAVASCRIPT:
Give your script an ID and then use parentNode to move up the DOM.
var the_div_you_want = document.getElementById("skript").parentNode.id;
JQUERY:
Pretty sure .parent() is what you're after
$('#scriptId').parent().attr("id");
Regardless, you need to find some way to locate the script and the move up the dom to find that relationship. Hope this helps.
http://jsfiddle.net/Zbuf8/1/
jQuery(document).ready(function($) {
$('div').attr('id');
});
should get you the value you need.

how can i call javascript function when clicking on image? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I call a js function so it work on click image?
$("#closeButton").click(function () {
$("#sheet").css("display", "none");
});
image code?
<img src="images/divclose.png" alt="*" onclick="function()"/>
Add an ID (remove your onclick thingy):
<img src="images/divclose.png" alt="*" id="closeButton" />
Write this (note that you don't need the outer function anymore if you already have it);
$(function () {
$("#closeButton").click(function () {
$("#sheet").css("display", "none");
});
});
Now, go reading a beginner tutorial, a book or whatever. Learn the basics ;)
You try to access the image using an id in your code, but you don't have any id on the image element. Just add that, and it will work (provided that you have your code inside the ready or load event, so that the element exists when it runs):
<img src="images/divclose.png" alt="*" id="closeButton"/>
Don't have any onclick attribute on the element if you want to bind the event using jQuery. The event model is somewhat different for jQuery, and may conflict with plain DOM events.
tag must have the "id" and you can use the same method you described
I assume you want to use jquery so you should check jquery documentation.
What you did is declaring an "oldschool" pure javascript way to call "function()" on the click.
Therefor you need to declare the function "function" in the javascript itself the old fashioned way.
Using jquery and your code all you need is to assign your id to your image.
<img src ="foo.png" id="closeButton"/>
and it should work.

click and if in Javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I've this function in my script :
$("#image4").click(function(){
if($(this).attr("src")=="http://XXX.jpg"){
$(this).attr("src","http://YYYY.jpg");
}else{
$(this).attr("src","http://XXX.jpg");
}
return false;});
HTML
<img src="http://XXX.jpg" id="image4"/>
I don't understand why when I click on my image, the if part doesn't works. I explain : If I use this code, nothing change. If I add and alert un my click function, I see the alert twice and see the image change twice to.
It seems, "if" doesn't works and apply "if" and "else" at each time
Someone can help me about that. I think it a basic feature, I don't understand why it doesn't works.
Thanks
("#image4").click(function(){
^^
you missed a bracket!
Working: http://jsfiddle.net/MAVyb/
The sysntax and everything on your code is right so it is definetly value problem. Problem may be src have leading or trailing space in it.
try alert like
alert("-"+$(this).attr("src")+"-")
and whether is there any space between -(hypen) and src value.
also try
alert(($(this).attr("src")=="http://XXX.jpg")), it should return true if the src is equal
Update: i can reproduce your problem -- you should have attached the event listener function for two times. check your original code. that is the problem. check your code. you can verify this by changing id to some other in jquery method and img tag

Categories

Resources