This question already has answers here:
How to get an HTML element's style values in JavaScript?
(5 answers)
Closed 6 years ago.
I have a problem with the following JS function:
function toggleSortierung() {
var form =document.getElementById("sortieren_form");
if(form.style.display=="none") {
document.getElementById("sortieren_form").style.display="block";
}
else {
document.getElementById("sortieren_form").style.display="none";
}
}
This is toggling the visibility of a form on and off. The form is not visible on default. The function is integrated into a link:
Sortierung:
The function actually works, but the very first click on the link is doing nothing, although it should make the form visible. After the first click the function works like expected, so it only hinders smooth usability.
put an inline style display:none in your form element.. so that it will return form.style.display as none on first click
Related
This question already has answers here:
Event binding on dynamically created elements?
(23 answers)
Closed 6 years ago.
when I click and blur div, an other div is create in last, but when I blur div what just is add, javascript not working, please tell me, why? and how fix.
Thanks for advance.
Sample is:
http://jsfiddle.net/a4QNB/420/
js:
var contents = $('.changeable1').html();
$('.changeable1').blur(function() {
$('#addItem').before('<div class=\"changeable1\" contenteditable=\"true\"> Click this div to edit it </div>');
});
The new div is getting dynamically added, so try to delegate the event.
var contents = $('.changeable1').html();
$('body').on('blur','.changeable1',function() {
$('#addItem').before('<div class="changeable1" contenteditable="true"> Click this div to add an other div </div>');
});
Note: You are using jquery 1.6. on is not available with this version
JSFIDDLE
This question already has answers here:
How to call multiple JavaScript functions in onclick event?
(14 answers)
Closed 7 years ago.
I was trying to run two JS functions in one click using this. Can anybody show me how to do it correctly?
<button onclick="getlocation" onclick="showDiv()">Try It</button>
I tried adding a ; between the two functions but it didnt work too.
"Adding ";" semicolon didn't work, if you could help.."
It works for sure, you have to do it like that: onclick="getLocation();showDiv()"
Was not relevant in this case:
The problem could be, that both functions are setting the content of your div, so the second function will overwrite the content!
Use .innerHTML += "..."; on you second function, this will append the content to the exisitng content.
This question already has answers here:
Onclick event getting called automatically
(3 answers)
Closed 7 years ago.
I have a script that makes that makes a tile for every item in an XML-file. The tile is a div inside an "a" tag created by javascript.
document.getElementById('Body').appendChild(tileLink);
tileLink.appendChild(tile);
tile.appendChild(tileTitle);
tile.appendChild(tileImg);
document.write(" ");
So when tileLink is clicked a javascript function "showDiv(divId)" has to be loaded. divId is a variable in the script needed to load the function.
I've tried these 2 lines but with both the script doesn't work and no objects are loaded.
tileLink.onclick = showDiv(divId);
textLink.addEventListener("click", showDiv(divId));
Where am I wrong?
Event handlers require function references. By invoking the function, you are immediately executing it.
textLink.addEventListener("click", function() {
showDiv(divId);
});
This question already has answers here:
How to reset a form using jQuery with .reset() method
(16 answers)
Closed 8 years ago.
I KNOW this question has been asked numerous times as I have used this website's solutions to no avail. I currently have this implementation:
$("input:reset").click(function () {
$('#answer_2').html('License number is not long enough');
$('#answer_1').html('');
$('#data_entry').each (function(){
this.reset();
});
});
I know the selector is correct as the two html changes (I put them in to confirm I was selector for the reset button click correctly) occur as they should.
Here is my form declaration:
<form name="data_entry" id="data_entry" method="post" action="">
The problem is I keep getting the error that there is no 'reset' function and the form is never cleared. This is my most recent attempt at following answers to this problem on stackoverflow site. Please help.
Why reset when you want to clear/empty?
this.empty();
might do the trick?
Your each is wrong and it's unnecessary.
$('#data_entry').get(0).reset();
This should work because you got the right element but $() returns a jQuery object, not a DOM element. get will grab your DOM element and then you can use reset() (which is not a jQuery function)
This question already has answers here:
Find currently visible div in jquery
(5 answers)
Closed 3 years ago.
Only one jQuery event runs per refresh of the page. What am I doing that's so wrong?
$("#contact_link_email").on("click",function(){
$("#contact-form").replaceWith('<h2>Email heading</h2>');
});
$("#contact_link_facebook").on("click",function(){
$("#contact-form").replaceWith('<h2>Facebook heading</h2>');
});
$("#contact_link_twitter").click(function(){
$("#contact-form").replaceWith('<h2>Twitter heading</h2>');
});
$("#contact_link_gplus").click(function(){
$("#contact-form").replaceWith('<h2>GPlus heading</h2>');
});
Once one of those events runs, $("#contact-form") doesn't reference anything. If you want to replace it with something, try making that something have the same id.