There are 7 Question in form with number 1.1 to 1.4 and 2.1 to 2.3, having Radio as answer(ID: #radio_1.1 and #radio_1.2) and textarea for comments(ID: #textarea_1.1, #textarea_1.2).
Questions are dependent on one another like question 1.4 answer if selected "yes" in radio, then will disable textbox of Question 2.2. Also, need to achieve this functionality during click as well as on load.
Following is way i am generating question id for now in JQuery/Javascript:
for(var tabNumber=1;tabNumber<=2;tabNumber++) {
var questions=0;
if(tabNumber==1) questions=4;
if(tabNumber==2) questions=3;
for(var decimalNumber=1;decimalNumber<=questions;decimalNumber++){
disableit(tabNumber+"\\."+decimalNumber,value); //This generate id of comment box like #textarea_1.1, #textarea_1.2 and radio also like #radio_1.1 and #radio_1.1 and even few classs like .Class_1.1 and .Class_1.2
}
}
in disableit we have functionality like:
$(".Class_"+number).addClass("yahooeffect");
$("#textarea_"+number).attr('disabled', true);
Is there any alternative in JQuery/Javascript which helps to remove for loop and do above operations in another simpler way? Any Regular expression which can help me like .Class_[1.1-1.9] etc, so that i can applu things in just one or two statements?
Update
Each Question is in one line. Like (1.1 - Do you want bird? YES/NO Comment) (1.2 - Do you want Doggy? Yes/No Comment). Each of these 4 in seperate div like div for 1.1 and div for question and div for radio and div for comment. So ID's are separated as explained in question. Questions have mixed dependency
Have elements have a common class. For example, class="question". Then you can target all of them using $(".question"). If they have unique IDs, use ID to target them individually: id="question-1.1" and $("#question-1.1"). If you can't use IDs, assign two classes: class="question question-1.1", then you can use $(".question") for all or $(".question-1.1") for one.
Try uniqueId
Example:
$(selector).uniqueId();
HTML:
<div class="checked">1</div>
<div class="checked">2</div>
<div class="checked">3</div>
<div class="checked">4</div>
<div class="checked">5</div>
<div class="checked">6</div>
<div class="checked">7</div>
<div class="checked">8</div>
<div class="checked">9</div>
<div class="checked">10</div>
<div class="checked">11</div>
<div class="checked">12</div>
JS:
$(document).ready(function () {
$(".checked").uniqueId();
});
Result:
<div class="checked" id="ui-id-1">1</div>
<div class="checked" id="ui-id-2">2</div>
<div class="checked" id="ui-id-3">3</div>
<div class="checked" id="ui-id-4">4</div>
<div class="checked" id="ui-id-5">5</div>
<div class="checked" id="ui-id-6">6</div>
<div class="checked" id="ui-id-7">7</div>
<div class="checked" id="ui-id-8">8</div>
<div class="checked" id="ui-id-9">9</div>
<div class="checked" id="ui-id-10">10</div>
<div class="checked" id="ui-id-11">11</div>
<div class="checked" id="ui-id-12">12</div>
Demo: http://jsfiddle.net/GCu2D/693/
Many widgets need to generate unique ids for elements. .uniqueId() will check if the element has an id, and if not, it will generate one and set it on the element. It is safe to call .uniqueId() on an element without checking if it already has an id. If/when the widget needs to clean up after itself, the .removeUniqueId() method will remove the id from the element if it was added by .uniqueId() and leave the id alone if it was not. .removeUniqueId() is able to be smart about this because the generated ids have a prefix of "ui-id-".
Related
I am trying to create a on-click function for each button, hence i need a way to select each one individually without using ids
<div class="col">
<div>
<b>Name:</b> <span>John</span> <button>change</button>
</div>
<div>
<b>Surname:</b> <span>Doe</span> <button>change</button>
</div>
<div>
<b>Email:</b> <span>doe#gmail.com</span> <button>change</button>
</div>
<div>
<b>Birth date:</b> <span>13 May 1947</span> <button>change</button>
</div>
</div>
You have a lot of selector that you can use if you don't want to use class or ids, even though I highly suggest you to use them whenever possible for the sake of comprehensibility.
Here, you could use the :nth-of-type(selector) like this.
// Select the first button of your page.
document.querySelector('button:nth-of-type(1)');
// Select the first button of your .col
document.querySelector('.col button:nth-of-type(1)');
Here is a list of selectors you can use
div b ~ button
div button
.col div button
You can try the selectors here: http://try.jsoup.org/~91Zdheyo7rX9PpORdECPjMxb890
I'm trying to access the second instance of 'div.question.question-dropdown input'. I have tried incorporating the typical :nth-child() and :nth-last-child() type pseudo selectors but they are being ignored. Below is the JS code I'm trying to achieve; but adding the unique Data to the second instance (but can't find a way to unique select).
.waitForElementVisible("div.question.question-dropdown input",400)
.click('div.question.question-dropdown input')
.setValue('div.question.question-dropdown input', Data.CreateOpp.motivation)
.pause(500)
.waitForElementVisible("div.question.question-dropdown input",400)
.click('div.question.question-dropdown input')
.setValue('div.question.question-dropdown input', Data.CreateOpp.motivation)
.pause(500)
.click('div.question-btn.question-btn-submit')
The mark-up is 2 different input form fields, within 2 div.question.question-dropdown.
<!-- first -->
<div class="question question-dropdown">
<div class="smart-input">
<label></label>
<input>
</div>
</div>
<!-- second -->
<div class="question question-dropdown">
<div class="smart-input">
<label></label>
<input>
</div>
</div>
In my opinion nth selector should work:
"div.question.question-dropdown:nth-of-type(2) input"
But if not, you can try XPath:
"(//div[#class="question question-dropdown"])[1]//input"
I have a question about dom navigation with jquery. I'm trying to find an element with a given class that is closest in the dom following a given element.
I have a table like structure, created through divs and styled in css. I have an element being edited, and when the user presses enter I want to focus the following editable element. However, it's not a sibling of the element being edited.
HTML
<div class="calendarEntry">
<div when="2014,9,18" class="when">Sep 18</div>
<div class="items">
<div class="item">
<div code="ABC" class="type">ABC123</div>
<div offered="2014,9,15" class="offered dateish">Sep 15
<div class="offer editable">10</div>
<div class="sku editable">TH1</div>
<button>Publish</button>
</div>
</div>
<div class="item">
<div code="DEF" class="type">DEF321</div>
<div offered="2014,9,14" class="offered dateish">Sep 14
<div class="offer editable">10</div>
<div class="sku editable">TH2</div>
<button>Publish</button>
</div>
</div>
<div class="item">
<div code="GHI" class="type">GHI852</div>
<div offered="2014,9,12" class="offered dateish">Sep 12
<div class="offer editable">10</div>
<div class="sku editable">TH3</div>
<button>Publish</button>
</div>
</div>
</div>
</div>
Note: There are multiple calendar entries on the page.
Say the user is editing the offer of the DEF312 item. When they hit enter I want to edit the offer of GHI852. I have the code to make the div editable, by replacing it with a text field with a class of offer editing. If they're editing the final offer in this calendar entry, then the enter key should focus the first editable offer of the following calendar entry, if there is one. If we're at the bottom of the list I don't want to wrap back to the top (which I think would overly complicate matters anyway).
The bit I'm stuck with is how to find the next offer (all offers are editable).
Here's what I've tried:
var nextOffer = $('.offer').find('.editing').next('.editable');
Clearly, this doesn't work. The problem is that the following editable offer isn't a sibling of the current offer being edited, so next() doesn't work for me. The following offer could be in the current calendar entry, or it's just as likely to be in the next calendar entry. Either way, it's a few divs away, at varying depths.
Can I even do this with jquery dom traversals, or am I better just brute forcing it through javascript (i.e. looping through all .editable instances and returning the one after .editing?
Adding the class 'editing' to simulate the the input:
<div class="item">
<div code="DEF" class="type">DEF321</div>
<div offered="2014,9,14" class="offered dateish">Sep 14
<div class="offer editable">10</div>
<div class="sku editable editing">TH2</div>
<button>Publish</button>
</div>
</div>
you can do:
function findEditable(currentItem) {
var nextEditable = undefined,
selectors = [".item", ".calendarEntry"];
$.each(selectors , function (idx, selector) {
var ref = currentItem.closest(selector);
nextEditable = ref.parent()
.children("div:gt(" + ref.index() + ")")
.find(".offer.editable")
.first();
return nextEditable.length === 0;
})
return nextEditable;
}
findEditable($(".editing")).css({
color: 'red'
});
jsfiddle demo
You can use parents() to get the .offered element which contains the .offer element like so:
var offered = $('.offer').find('.editing').parents('.offered');
From that you can use next() to get into the .offered element's sibling .item element, and find the .editable element within that:
offered.next('.item').find('.editable');
JSFiddle demo. Note that I've manually added this .editing element within your DEF321 item's .offer element - I assume this gets added dynamically on your side, but either way isn't included in your question.
Edit: The HTML in the question has now been changed. Based on this, instead of getting the .offered parent, you'd get the .item parent:
var item = $('.offer').find('.editing').parents('.item');
And proceed in the same way as before:
item.next('.item').find('.editable');
JSFiddle demo.
try this
var current=document.activeElement,
all=$(".editable"),
index=all.indexOf(current),
next=all[index+1]
It first finds the current element and the list of elements,
then it will find the current element in the list.
It will then add 1 to the index and select it from the list.
To extend the array with the indexOf function;
if(!Array.prototype.indexOf){
Array.prototype.indexOf=function(e/*,from*/){
var len=this.length>>>0,
from=Number(arguments[1])||0;
from=(from<0)?Math.ceil(from):Math.floor(from);
if(from<0)from+=l;
for(;from<len;from++){
if(from in this&&this[from]===e)return from;
}
return -1;
};
}
I have the next code dynamically created using JQuery. Theere are multiple row class divs placed one under the other.
<div class="row">
....
</div>
<div class="row">
<div class="line_type"></div>
<div class="download_value"></div>
<div class="flag"></div>
<div class="email"></div>
<div class="prize"></div>
</div>
<div class="row">
....
</div>
After i create these divs I have a "pointer" to a specific div which is of class row. In JQuery, how do i make it so I go down the DOM tree, until i reach the div of class line_type and download_value and remove them both, and also I'd like to go one more node down, at the div of type email and change some of it's CSS attributes.
I was not able to find anything on the web, maybe it's cause i'm a noob at these still.
I have a "pointer" to a specific div which is of class row ->
Assuming that you have the this object of the corresponding div with class row.. then you can use .find to get the line_type and download_value inside that div.
$(this).find('.line_type').remove();
$(this).find('.download_value').remove();
Then you can use the same .find to get the div with class email and access the .css
$(this).find('.email').css(/* You code*/);
Assuming row_pointer points to the row in question:
$('.line_type, .download_value', row_pointer).remove();
$('.email', row_pointer).css(...);
check this out
$('div.row').bind('click', function() {
$this = $(this);
$('div.line_type, div.download_value', $this).remove();
$('div.email', $this).css('background-color', 'red');
});
http://jsfiddle.net/YvyE3/
I have a page with two divs in it, one inside the other like so:
<div id='one'>
<div id='two'></div>
</div>
I want div one to change class when it is clicked on, then change back when div two is selected.
I'm completely new to javascript, but I've managed to find a simple command that makes div one change when I click it.
<div id='one' class='a' onclick="this.className='b';">
<div id='two'></div>
</div>
Now I just need an equally simple way to change div one back when number two is clicked.
I've tried changing "this.className" to "one.classname," and for some reason that worked when I was working with images, but it doesn't work at all with divs
<div id='one' class='a' onclick="this.className='b';">
<div id='two' onclick="one.className='a';">
This does not work.
</div>
</div>
Essentially I'm wondering if there is a substitute for the javascript "this" that I can use to target other elements.
I've found several scripts that will perform the action I'm looking for, but I don't want to have to use a huge, long, complicated script if there is another simple one like the first I found.
You can use document.getElementById
<div id='two' onclick="document.getElementById('one').className='a'; return false;">
This does not work.
</div>
This would work:
document.getElementById('one').className = 'a';
you could get the element by id with:
document.getElementById("one")