Modify HTML according to provided JQuery Snippet - javascript

I am trying to modify my HTML for according to class names in following jQuery Snippet. The snippet add class to texarea which turns border red
JQuery Snippet:
$(".scope-question .commentbox").removeClass("redColor")
$(".scope-question input[data-require-comment]:checked").each(function() {
$(this).closest(".scope-question").find(".commentbox").addClass("redColor");
});
Fiddle:
https://jsfiddle.net/t23un23y/
There are four questions, with 3 radio box as answer(Yes/No/Not Applicable) and a textarea for remarks. On Click of particular radio, Say NO, the textarea border turns red.
I am trying to modify HTML, using class names given in above snippet. Unable to get desire output. Could anyone please help in same.
Update:
1) if not necessary; please try not to modify JQuery code; but HTML Code as on particular click i need some changes in whole question row like grey of question, disable other radio etc.
2) I have just created fiddle for onClick of radio, i have use case to validate red color textarea on load too. So please also consider the case

Updated Fiddle: Here
Basically, your code assumes that .scope-question is a parent of the inputs, when in fact it is a sibling of their parents.
$('input[type="radio"]').click(function () {
var textarea = $(this).closest("div").next(".scope-question").find(".commentbox");
if($(this).val() === "No") {
textarea.addClass("redColor");
}
else {
textarea.removeClass("redColor");
}
});
Update
Detailed Fiddle
Here's a summary of the changes I made:
Updated the HTML to use .scope-question as a wrapper
Added data-require-comment attribute to the "NO" radio buttons.
I'm not sure the exact use case you want from this, but the code you provided suggests that this attribute indicates that comments are required (duh). Because you indicated that "NO" should turn the textarea red, I attached it to know. This of course suggests that if a user clicks yes, it's ok not to add a comment. You can modify this however.
Added a call to the update function for onload that checks all of the comment-required inputs that are checked to see if they have a comment in the comment box.

Here is your updated JSFiddle
Your only had to remove javascript each function and change few selectors

Related

Custom action links in .PDF...write Javascript to alter the appearance of links when clicked?

I have a .pdf document that contains custom links which run Javascript code.
There is no issue with the actual functionality of the working portion of the JS, but I do have one formatting/display problem that I havent been able to solve:
Is it possible to write JS that will alter the appearance of individual links as they are clicked?
I know I can programmatically change the appearance of all links on a page by looping through the doc.getLinks result and applying formatting changes to each element of the getLinks array. But I don't know how to refer to a specific link, as/after it's clicked, either by referencing that link's index location within the getLinks array, or by referring to it by any other name, handle, etc.
I would think that this is probably possible to do, but I'm at a loss.
Thanks in advance for any pointers!
EDIT: One thing to clarify...I can do everything I need to do for a single button. That is, I can manually find the button name, and manually enter the JS code to change the appearance of that particular button. To do this, I need to physically look up the name of the button using a few mouse clicks, and then hard code that button's name in my JS getField command. This requires different code for each and every button.
Is it possible to accomplish the same function using the same code for each and every button?
My ultimate objective is to be able to reproduce this function on a series of .pdf files that will, jointly, have thousands of individual buttons. So any manual component of this process will make implementation impractical.
I should have originally phrased the question in terms of, is it possible to write JS code that can automatically detect the name of the button which is calling the code? (ie, how would I implement a self-referential feature for a generic button?)
As wished by the OP…
When a script should refer to the field on which it is running, the field object to use is event.target.
An example:
You have a button which, when clicked, should change the width of the border between 1 and 3. The mouseUp event would containt this piece of code:
if (event.target.lineWidth == 1) {
event.target.lineWidth = 3 ;
} else {
event.target.lineWidth = 1 ;
}
Or another example: when the number in the calculated text field is negative, it should be in red, otherwise in black:
In the Format event of that field, you would add:
if (event.value*1 < 0) {
event.target.textColor = color.red ;
} else {
event.target.textColor = color.black ;
}
And that should give an idea on how to use event.target.

custom css error class not applying for the fields jquery validator

I have a form which contains two file input and two check box and one submit button. With my current code when the user click submit button the jquery validation will work perfectly but the problem was the custom error class was not applying and not removing correctly for example if the user click the check box that particular errorClassshould be removed this was not happening. When i search through SO I got one use full information it was mentioned instead of using border red for the check box or if the upload any file that particular field errRed class should remove and I try using outline-color but this was also not working. It might be simple somewhere I am missing some bit of code
I tried giving $('.chk_requ').removeClass('errRed_chkb'); still no use
Here is the fiddle link
kindly please help me.
Thanks in Advnace
Instead of remove class you can add empty div with id="diverr" there and give it desplay:none;
$("#diverr").text("Please select Gender");
$("#diverr").css("display","list-item");
if condition is false then
$("#diverr").css("display","none");

Display a div based on a checkboxes' value

I have a div in a form (#sides) that I only want displayed when one checkbox (.opp-choice-checkbox) is not checked. Here's the code I have right now:
jQuery ->
if !$(".opp-choice-checkbox").checked
$("#sides").hide()
$(".opp-choice-checkbox").click ->
$("#sides").slideToggle()
A similar question is asked a lot, but the difference with my question is that when I go to edit the object the form is for, sometimes the checkbox is checked by default. In that situation, I want the div to be showing when the page is loaded. The code I have right now hides the div by default, regardless of whether or not the checkbox is checked
UPDATED
Your code working perfectly with the little update mentioned below, See Working fiddle for your code:
CoffeeScript :
You have just to add the else in your condition to show the div if the checkbox is checked :
jQuery ->
if !$(".opp-choice-checkbox").is(":checked")
$("#sides").hide()
else
$("#sides").show()
$(".opp-choice-checkbox").click ->
$("#sides").slideToggle()
If sometimes you have a chekcked checkbox by default, you have just to remove condition and replace your coofee code by the following :
jQuery ->
$(".opp-choice-checkbox").change ->
$("#sides").slideToggle()
See fiddle HERE.
I hope this will help.
$(".opp-choice-checkbox") doesn't have a checked property - that's a jQuery element. To get the regular DOM elem, use [0]
$(".opp-choice-checkbox")[0].checked
Or, use .is("checked")
$(".opp-choice-checkbox").is("checked")
If this is all attached to a change or click event, just run that event on load to trigger the behavior, ($(elem).change(function() { }).change())

Blur event not firing on <label> - can't find workaround for dealing with hide-on-blur <input> text field

TL;DR how can I get this self-explanatory JSFiddle to work?
From the W3C:
The blur event occurs when an element loses focus either via the pointing device or by tabbing navigation. This event is valid for the following elements: LABEL, INPUT, SELECT, TEXTAREA, and BUTTON.
The basic idea, HTML:
<form>
<label>
<input type="text" />
after focusing in input, there should be no blur when clicking here
</label>
</form>
but blur should fire when clicking here
And JS:
$("form, label").on("blur", function() {
alert("you're not going to see this");
});
It doesn't work. A more illustrative example is in this JSFiddle.
I also tried focusout, with this JSFiddle, but (presumably because it bubbles up from the input), it always fires.
I could probably rig up what I need with a hack like this: https://stackoverflow.com/a/5049387/458614 but I'd rather not have to.
Edit: There are lots of related questions and I have read all that I could find, none of which help. Some talk about setting tabindex=0 on the form or label elements. I have tried this in various permutations but it doesn't help. JSFiddle here. If you put it on the form, blur events do fire when you click outside the form. However, it doesn't apply to any of it's children: it won't pick up anything if you click on the input and then outside the form.
Edit 2: I don't really understand some of the answers posted so far and none seem to really... work. Anyway, to clarify, here is what I am trying to accomplish:
In my app, you can add tags to documents. When you click the "add tag" button, a previously-hidden text input field pops up and is focused. And then...
Clicking outside (on blur) should close the text input field again
Pressing enter should add the tag and close the input field
Clicking the "add tag" button should also add the tag and close the input field
The problem is that #1 and #3 are incompatible. The "add tag" button needs to perform a different action based on whether the text field is open or closed, but because I can only achieve #1 with an onblur event on the text field, the text field is closed by the time any action happens on the "add tag" button for #3.
Here is a JSFiddle with my best attempt so far.
The thing I think you are looking for is
e.stopPropagation();
This Fiddle here shows a little different way to handle it ... it put the hide on a window click (which would blur the input anyways) except on the label, which it would allow the click event to stop inside the label.
Happy coding!
use the below code to achieve the desired
$(document).on("blur", "label",function() {
$("div").append("form or label blurred<br>");
});
Here is the demo Fiddle
Try this it should work
.focus {
border-color:red;
}
$(document).ready(function() {
$('input').blur(function(){
$('input').removeClass("focus");
})
.focus(function() {
$(this).addClass("focus")
});
});
Add this piece of js in your Fiddle. you added listener for label but blur happens on anchor tag.
$("form a").on("blur", function() {
$("div").append("form or label blurred<br>");
});
according to your explanation i have create a demo
$("form > label >a").on("blur", function() {
return false
});
$("#outsideform > a").on("blur", function() {
alert("but blur should fire when clicking here");
});
Check the Demo here
For a while, I am posting an intermediate development. But this definitely will help you where exactly you should look for. The jquery implementation but not your javascript.
This is the real concern.
I have added 3 lines at different places. no big changes.
Added an || $("input").css("visibility") == "visible" to the if
condition
Added $("input").css("visibility","hidden"); to the inner else condition
$("input").css("visibility","visible"); to the outer (and last) else condition.
Please note this is intermediate, you need to click twice after a submit of non-empty text.
If I get time, I would post the correct working thing.
This is the fiddle.
tobek, your JSFiddle with my best attempt so far is almost there. The problem is your selector at the bottom in this section of code:
$("input").on("blur", function(){
$("input").hide();
});
You stated the problem correctly in your comments when you said: "THE PROBLEM: we never get in here because it's already been hidden because the input blurred".
Change the above section to this and I think you'll have what you're looking for.
$("input-blur label").on("blur", function(){
$("input").hide();
});
Because the "Add tag" link is inside the label clicking it doesn't trigger your "blur" function.

Changing style of each empty element

I am working on a project where a user can add items i have provided them a "add another" button so that they can add another item so now they can add more then one items on the same time.
To validate this form and to insert values in db I have used the following procedure
Now I am accessing the form elements through its name
but my client wants to change the background color of each element the is left blank by the user. I have tried Jquery "empty", "nth child" selectors but i am totally failed to do this
so please help me to solve this problem
Are you only looking to change the background color of empty text boxes? If so, you could do:
$("#my_form input:text").each(function() {
if(!$(this).val()) {
$(this).css("background-color", "red");
}
});
You iterate over all the elements you want validated in #my_form and check to see if they have any values. If not, apply your CSS changes.
Use selectors $('#form input:text[value=""]').css('background-color','red');

Categories