FAQ Database Discussion Community


How can I apply jquery selector to only visible checkboxes?

jquery,jquery-selectors,visibility
I have the following code that checks all checkboxes in a html table column $('.selectAll').on('click', function (e) { if ($(this).is(':checked')) { $('input[type=checkbox][value=' + $(this).attr("id") + ']').prop('checked', true); } else { $('input[type=checkbox][value=' + $(this).attr("id") + ']').prop('checked', false); } }); which works great except in certain cases some of the rows of...

action on parent's sibling in jQuery

javascript,jquery,jquery-selectors
Here's my html code: <div class="btn-group js_publish_management> <button class="btn btn-danger js_publish_btn">Not Published</button> <button class="btn btn-success js_publish_btn">Published</button> <button type="button" t-attf-class="btn btn-default dropdown-toggle" data-toggle="dropdown"> <span class="caret"></span> </button> <ul class="dropdown-menu" role="menu"> <li> <a href="#" class="js_publish_btn"> <span...

Can't select span inside div (jquery) [closed]

jquery,html,jquery-selectors
I can't seem to select the span inside the image below with the class 'star-rating-control. I'm trying to move it to one of the labels above (which I can select) Can anyone help? I've tried the following: var test = $("span.star-rating-control"); var test = $("div.write-review form#reviews span.star-rating-control"); If any other...

JQuery: Select all ids ending with specific name and change its siblings input button

javascript,jquery,jquery-selectors
I'm trying to indicate that a text can't be sent just yet, showing a grey input submit button with the class btn-primary. Instead btn-default is a coloured input button. However there are many textareas on one single site as my JQuery code should be used for both posting a new...

Is there any way to get the closest parent which have a specific class

jquery,jquery-selectors
In my application I have to get to the closest parents of the input/textarea which has the class of ('form-group'). I use the following code: $("#contactus").validate({ highlight: function (element, errorClass) { $(element).parent('.form-group').addClass('error-sign-up'); } }); For the cases of element which is input the code is fine (the direct parents of...

Jquery onchange event not triggering

javascript,jquery,html,struts2,jquery-selectors
I am stuck in a situation where I have a Struts2 form with a select tag which when changed should trigger an event and I can't figure out why the onchange function is not triggering but the peculiar thing is that the onchange trigger event works in an other example....

Select a specific number of items of a specific type which are brothers with jQuery

jquery,jquery-selectors
I wish to add a class to pictures in a p only if they're strictly 2 pics in the p (not an alone img or more than 2), with a jQuery selector. <div class="wysiwyg"> <p> <img class="addHere"/> <img class="addHere"/> </p> <p> <img/> </p> </div> For now, I have something like...

jquery selector: child elements of the same class inside divs of different classes

jquery,html,css,jquery-selectors
There are two divs with classes ".div1" and ".div2". Each of them contains the same code - the same elements with the same class names. If I need to set/get a value of an element inside one of the divs, how do I refer to those elements? I try the...

CSS or jQuery selctor? [closed]

jquery,html,css,jquery-selectors,css-selectors
Let's say there is the following HTML: <div id="test"> <span class="testSpan"></span> </div> There are two ways of selecting the span inside the #test div (I know I can select the span directly but this is not the point of the question): $('#test > .testSpan'); and $('#test').find('.testSpan'); Which of these ways...

Remove empty

, but allow only one per group

javascript,jquery,jquery-selectors,is-empty
I was wondering how to remove empty elements, but allow only one empty element per group (next to non-empty one) ? Something like : Example HTML : <p>Hello world</p> <p><br></p> <p><br></p> <!--This will remove--> <p><br></p> <!--This will remove--> <p>Lorem ipsum dolor sit amet</p> <p><br></p> <p><br></p> <!--This will remove--> <p>Eum ne...

Stored selector doesn't work correctly with each()

jquery,jquery-selectors
Below is a recreation of the problem I'm having. I stored $('.box') into the $box variable. In my actual code, I'm using $box in many places to do things and it works fine, except in the portion of code below, where I tried to use loop through the many $box's...

How to check if TD is last with class in row

javascript,jquery,jquery-selectors
I am new to jQuery and hope someone can help me with this. I have a large HTML table with dynamically added rows. The table has a standard structure (incl. thead, tbody and tfoot). Within this table there are editable TDs (which have the class "editable" and contain a contenteditable...

Why jquery nth child give inconsistent result when apply to ul and li

jquery,jquery-selectors
I have a container with a group of uls, of each have 4 lis. I created the first ul then copy the rest by Javascript. When I call to find nth-child of all uls, eg $("ul:nth-child(1)"), it return nothing, the first ul item appear with $("ul:nth-child(2)") (n is starting from...

How to change the color of first option in select using jQuery

jquery,jquery-selectors
I am having the following Html <select> <option value>--Select--</option> <option value="60">Salaried Employee</option> <option value="61">Trainee</option> <option value="62">Skilled Worker</option> </select> Now here the my requirement is to display the first option in a blue color and the rest in black color. The first option should be blue in color either selected or...

jQuery button to show/hide multiple divs with same id

jquery,jquery-selectors,click,element,slidetoggle
I have many products being populated dynamically, all with same id and class names. I want each "Buy Now" button to have its own div that toggles with click of button. I do have the ability to assign an incrementing number index to the ids and classes, however, if there's...

How do I get the value in this neighboring element?

jquery,jquery-selectors
I'm having trouble with the jQuery part: HTML <div> xxxxxxxxx <div> <ul> <li><button class="addnum">xxxx</button></li> </ul> </div> </div> <div class="num"> 12345 </div> Script $(".addnum").click(function() { var pur = $(this).siblings(".num").clone(); }); I'm trying to clone the value inside ".num", which is 12345. How should I select it? *edit: sorry, I didn't mention...

How to find an element by data attribute with find() function?

javascript,jquery,jquery-selectors
I add a data attribute to an element via jquery data() function. I want to use find() function to get the element. But obviously, it does not work. What I want to do is caching the element's parent element and do a lot of things. Like this: var $parent =...

Get just class names from string selector, jQuery way?

jquery,jquery-selectors
Suppose I have string selector such as: input.js-pretty-select[type="radio"].selected How can I get just all class names (js-pretty-select and selected) from this string, possibly using some jQuery core function? I thought about some regex at first, but then I realized that jQuery might have that already implemented somewhere in it's code,...

Jquery.on('click') does not work on “class selector”

jquery,jquery-selectors
The following code http://jsfiddle.net/HCUYW/100/ does not emit click event. $(".todoitem").on('click', function() { alert('Item selectedd'); }); The workaround is to use the click method directly as $(".todoitem").click(...). How to make the .on('click',...) work on class selector ?...

Passing multiple jQuery objects as an array

javascript,jquery,arrays,object,jquery-selectors
I'm wondering if it's possible to do something like this in jQuery: $( [ $selector1, $selector2, document.getElemetByID( 'test' ) ] ) EDIT: I'm lazy. $selector1 and $selector2 are already jQuery objects. I am trying to avoid using $.each. Here's a more in-depth example of what I'm trying to do: var...

jQuery XOR selector

javascript,jquery,jquery-selectors,xor
From a set of divs I want to select all the divs that contain a specific attribute and then select all the divs that do not contain any of the attributes. Example: <div data-attr="1"></div> <div data-attr="2"></div> <div data-attr="3"></div> <div data-attr="4"></div> <div data-attr="5"></div> var attrList = [3,4]; // I want to...

jQuery .not() selector not working with class [closed]

javascript,jquery,jquery-selectors
I'm trying to select all of the direct children of body, except for those that match a certain class. Whether I use the .not() filter or the :not() selector, the element I'm trying to filter out is STILL part of the selection when I filter by class name. However, if...

trigger a click on multiple elements with the same class

javascript,jquery,jquery-selectors
I'm trying to trigger a click on multiple elements with the same class but when i do so the first element only get clicked and not the others, frankly I'm trying to make likes to all ask.fm profile answers by using the console of firefox so this is what i...

jQuery selector for second column in HTML table row

javascript,jquery,html,css,jquery-selectors
The aim of my code is to add the data from the selected HTML table row to the JavaScript array, and remove it if the row is unselected. https://jsfiddle.net/nicktry/h1636ram/ I have two problems at the moment: The CSS Style is not being applied on the 1st click I can't get...

change color when a country selected

javascript,jquery,css,jquery-selectors
I am trying to change color of a telephone field when a country is selected on the <select> options. For example if someone selects UK the color must change to Red. But I'm not sure what I'm doing wrong. This doesn't change at the moment. jQuery('select[name="address[_item1][country_id]"]').change(function() { if (jQuery(this).val() ==...

JQuery how to select parent but not children

jquery,jquery-selectors
Sorry for what is probably an easy question, but I haven't found a way to make this work yet. I'm new to JQuery and I've got some nested lists which I'm trying to add a click event to a parent list which will slideToggle the children lists, which I've got...

Using a jQuery Selector With a Variable

javascript,jquery,variables,jquery-selectors
Is there any way to make something like the following code? var input = ("$(\"#div .input[id='" + id + "']\")"); input.fadeIn(); Or maybe I could use something like the following? var input = ("\"#div .input[id='" + id + "']\""); $(input).fadeIn(); PS: Take my apologies as this question was cloned but...

Get the next div and check that div for a class exists in it's childs

jquery,html,jquery-selectors
How do I determinate if one of the buttons have the class "selected"? <a id="foo">Foo</a> <div id="div"> <input type="button" /> <input type="button" class="selected" /> <input type="button" /> </div> I wrap foo into a jQuery object. Call next() to get to the div. How do I do the rest? function fooClickHandler(foo)...

Select word between two words

javascript,arrays,jquery-selectors
How can I create a function that selects everything between the words X and Y and pushes it to an array. By Greili - 4 Hours and 40 Minutes ago. #NsShinyGiveaway 0 comments By ToneBob - 4 Hours and 49 Minutes ago. #NsShinyGiveaway 0 comments By hela222 - 5 Hours...

`div input[type='checkbox']:lt(2)` only selecting first row

jquery,html,jquery-selectors
I want to cause an alert when the user clicks on either of the first two <input>s in each row, but my current code is only working for the first row. How can I fix my selector to work for the rest of the rows? HTML: <div> <input type='checkbox' />...

Select specific element type with specific ID in jQuery

javascript,jquery,jquery-selectors
I want to find any canvas elements with the id whatever using jQuery. I thought I could do $('canvas #whatever') but that doesn't return anything when I have a canvas with that id on the page.

Select the first-of-type div in a list that all have a data-attribute for the state it's associated with

css,jquery-selectors,css-selectors
I have a dynamically created list of elements that are job's in a particular state. There can be more then one job per state and I'm trying to select the first one of each state, but not just the first one in this huge list. For example: <section class="careers"> <div...

How is this selector working?

javascript,jquery,jquery-selectors
In the HTML I have the following element: HTML <input id="userNameTxt" type="text" class="input" value='ABC> And i am selecting it like this in jQuery: APP.js $(userNameTxt).val() What is this selector selecting? (Like when I use $('.something') -> it's selecting the Class of the element or $('#something') it's selecting the ID.) It...

what is differences between $('element , #id') and $('element', '#id') in jquery

jquery,jquery-selectors
I know $('element, #id') selects all elements & #id. But what does do $('element', '#id') !? I only know they aren't similar. So what's difference?

jQuery li element select by ID

javascript,jquery,html,jquery-plugins,jquery-selectors
can anyone please help? I have a HTML code like this. <ul class="nav navbar-nav navbar-left"> <li> <a href="#"><img src="img/topbararrowback.png" alt=""></a> </li> <li id="hide_filter"> <a href="#">Hide Filter</a> </li> </ul> I try to add a .click event on li having id hide_filter. What I have done is- $("#hide_filter").click(function() { alert('message'); }); And...

How do I select a class but not its inside element?

javascript,jquery,jquery-selectors,selector
Say I have <div id="mydiv"> <div class="myclass"> <span class="otherclass"></span> and many other classes... </div> </div> I want to capture the click event on .mydiv but not inside .myclass. I tried .mydiv:not(.myclass) but it doesn't seem to work. I think it's because I might be clicking on the otherclass so the...

jQuery selector: Selecting a certain combination of classes when multiple exist

javascript,jquery,jquery-selectors
I have tried to do some research already and haven't found anything, but If I've missed the answer, please tell me. Basically I have multiple elements with multiple classes each, but the combinations are unique. I want to select a certain combination of classes, but not other elements that have...

jQuery: How to combine two lines of code (with same target)

jquery,jquery-selectors
I am new to jQuery and am trying to combine the following two lines of code as they both target the same div ( $(this).closest('div.col-9').nextAll('div.hiddenDiv') ) and I have other, similar scenarios where this would reduce code as well: $(this).closest('div.col-9').nextAll('div.hiddenDiv').find('input, select, textarea').removeProp('checked').removeProp('selected').not(':button, :checkbox, :hidden, :radio, :reset, :submit').val(''); $(this).closest('div.col-9').nextAll('div.hiddenDiv').hide(); It works...

jQuery selector next child of nested elements

javascript,jquery,html,twitter-bootstrap,jquery-selectors
How to select the next input within nested elements? For example: when currently selecting 1a, pressing the down arrow key should select 2x, and down again should select 3x, and so on until 6x. In the other case, if currently selecting 1a, press the right arrow key should select 1b....

How to extend a jQuery selector that is stored in a variable?

jquery,performance,jquery-selectors
I have a jQuery selector stored in a variable, like so: var myVariable = $( ".js-selector" ); I'm wondering if and how I can use this variable to select children of this element, instead of writing: $( ".js-selector .some-child" ); How do I do this? I'm also wondering if I...

Get First Span of a
using the data-id

javascript,jquery,html,jquery-selectors,jquery-ui-draggable
I want to get the first span element of a div. This is how i get the div DOM element. helper: function(event,ui){ var dataId = $(this).data('id'); console.log($('[data-id="' + dataId + '"')); } NOTE Im using Draggable plugign from jquery-ui The console.log prints correct the div element, but now i want...

Add class to nth of an element found on full page instead within a parent

jquery,jquery-selectors,element,addclass
I'm trying to add classes to the "nth" of a particular element - regardless of where it falls on the page. A simplified version of my html is: <div class="row"> <div class="col-sm-12"><section class="subsection"></section></div> <div class="col-sm-12"><section class="subsection"></section></div> </div> <div class=“row”> <div class="col-sm-12"><section class="subsection"></section></div> <div class="col-sm-12"><section...

select special selector after $(this) selector

javascript,jquery,html,jquery-selectors
for example I have this code <script> $(document).ready(function () { $('span').each(function () { $(this).html('<div></div>') ; if ( $(this).attr('id') == 'W0' ) { $( this > div ?!!! ).text('0') } if ( $(this).attr('id') == 'W1' ) { $( this > div ?!!! ).text('1') } if ( $(this).attr('id') == 'W2' ) {...

How do you select in-line text with Jquery?

javascript,jquery,jquery-selectors
How can I select the text infront of the unordered list below? <li class="foo"> How can I remove this text? <ul class="bar"> <li><a href="#">Link</a></li> </ul> </li> I tried doing something like this: $('ul.bar').previousSibling().remove(); But I think that would only work with another element, and I'm not entirely sure how to...

Assigning onclick via jQuery selector

javascript,jquery,jquery-selectors
I am trying to assign an anchor's onclick via javascript - see jsfiddle below: <a id="link_a" class="linka" href="http://www.google.com">link 1</a> var lnkA=$('.linka'); lnkA.onclick=function(){alert(this.innerHTML);}; fiddle: https://jsfiddle.net/andrewtr/tkfvyr7o/ Code: However, it doesn't seem to work. What am I doing wrong? Thanks!...

Strange behaviour with jQuery pseudo selector :visible

javascript,jquery,jquery-selectors
I have a query that looked like this: $("div.modal:visible select#DTE_Field_calculator\\.type") which would attempt, in theory, to find that <select> tag inside of the currently visible div.modal element. Unfortunately, it would return nothing at all, despite the DOM looking exactly as you would expect (<div class="modal">...<select id="DTE_Field_calculator.type">...) If I removed the...

how do I set .css backgroud url of a select box in jquery?

jquery,jquery-selectors
I have a select box and I would like to set its background url in jquery jQuery(document).ready(function($){ $('select').css({ ("background", "url("+site_url+"/img/select-arrow.png)") }); }); This code doesn't work...

How to select a single cloned element?

jquery,jquery-selectors,clone
I am dynamically adding a div to my webpage when ever I click a button. I am doing this by cloning an original div and appending the clone. I have the above figured out. However, within the cloned div there is a text input. I want to make sure the...

Jquery selector not working with dynamic .get loaded content

jquery,html,jquery-selectors,selector
I have a page that dynamically loads content with a .get request from clicking a button. The HTML page that loads I can not get any jquery selector to target anything on that page. I tried putting the script for the alert on the page that gets loaded as well...

jQuery append using nth-child instead of first

jquery,jquery-selectors
I'm editing a script that was originally like $('.navigation-area:first').append('<li>New Item</li>'); However the application has since changed and this item needs to be appended to the second .navigation-area. I've tried: $('.navigation-area:nth-child(1)').append('<li>New Item</li>'); but that didnt work. Are there any other suggestions to how to do this? I would've assumed the above...

Syntax error with jquery selecting table row with a specific attribute

javascript,jquery,jquery-selectors
I am trying to pull the data out from a table row so that it can be updated, but I am really confused as to why it is not happening. Below is a snippet of generated HTML. The record ID is held in the first data element as the value...

Why does jQuery's :contains count unexpected in my code?

javascript,jquery,jquery-selectors
I have a function that takes two parameters: The first is a string to count, the second the id of an HTML element in which the function is supposed to count occurrances of that string. function countString(searchString, elementToSearchIn) { return $("#"+elementToSearchIn+" *:contains("+searchString+")").length; // In this example, call will be $("#searchElement...

Embedding a function inside .delay() doesn't work porperly

jquery,html,html5,css3,jquery-selectors
So I have HTML with a table and each column has button which should fade out that table and then fade in another DIV which contains info regarding about that specific column I clicked on. so the Jquery looks like this: $(document).ready(function(){ $('#motoBttn').bind('click', function(){ $('#ServicesContent').fadeOut(); $('#infoSlector').fadeIn(); $('#motoDescContent').fadeIn(); $('#goBackDiv').fadeIn() ; })...

Correct way to add variable and selectors to a not function - jquery

javascript,jquery,arrays,variables,jquery-selectors
I have checked a few answers on SO and I think my syntax is correct, but something is not working correctly. I am trying to create an array of jquery selectors mixed with variables and store them in a not() function. I have this fiddle to demonstrate. The second line...

Find descendant elements that do not have a specific ancestor

jquery,jquery-selectors
I want to be able to select an element and find all descendant elements with a specific class but are not contained with a similar parent element. It's hard to describe: Html: <div class="js-test js-1"> <div class="b">1</div> <div class="b">1</div> <div class="d"> <div class="b">1</div> </div> <div class="js-test js-2"> <div class="b">2</div> <div...

jQuery with the plus sign

javascript,jquery,html,jquery-selectors
I wonder why jQuery doesn't allow "+" sign. Here is an example of how it works with "1" and "3" but not with "2+". Just mouse-over the text above every div. <div id="div-2+"></div> JSFiddle $('a.hover').mouseover(function() { dataI = $(this).data('i'); $('div#div-' + dataI).addClass('h'); }); $('a.hover').mouseout(function() { dataI = $(this).data('i'); $('div#div-' +...

Jquery: how do i apply Empty Text to all textboxes, not just 1

jquery,asp.net,jquery-selectors
I have this script which will put the tool tip text from a textbox as it's "Empty Text". Then when you click on it, the empty text disappears and reappears if you Blur and the textbox is still empty. var $textbox = $("#MainContent_tbCity"); // select single box (how get all?)...

How to select an element by it's value in jQuery?

jquery,jquery-selectors
I have the following HTML-source <tr> <td>Peter Pan</td> <td>Nervland</td> <td> <a href="edit/id-for-peterP">edit</a> <a href="delete/id-for-peterP">delete</a> </a></td> </tr> <tr> <td>Wendy</td> <td>Nervland</td> <td> <a href="edit/id-for-wendy">edit</a> <a href="delete/id-for-wendy">delete</a> </a></td> </tr> The task is to choose the edit-link in the row that also contains a...

Hot to get the text of the selected option in jQuery?

jquery,html,jquery-selectors
td is a jQuery variable that references a table cell <td> element. This cell contains a <select> element which contains a number of <option> elements; each has a text and value. This code returns the value (not text) of the selected option. td.children().val(); I tried many ways to get the...

Not able to access click event with jQuery selector

javascript,jquery,html,jquery-selectors
I am having a hard time accessing a click even with my jQuery selector. Here is my HTML: <li class="handle-bars"> <span class="fa fa-bars pull-right text-primary m-t-10"></span> <span class="fa fa-cogs pull-right text-primary m-t-10" id="trigger-modal-editor" style="cursor:pointer" data-target="#modalSlideLeft" data-toggle="modal"> </span> </li> Here is my jQuery selector: <script> $(document).ready(function() { $('#trigger-modal-editor').click(function() { alert('Hello'); });...

Get group radio button selected value via jquery

javascript,jquery,html,jquery-selectors,radio-button
How can I get the selected radio button value via jQuery? I tried: var selectedPlan = $("input[name=Plan[packageId]]:checked'").val(); alert(selectedPlan); bBt didn't work. <td style="vertical-align: top;"> <input type="radio" value="1" name="Plan[packageId]"> <lable>3 Games</lable><br> <input type="radio" value="2" name="Plan[packageId]"> <lable>5 Games</lable><br> <input type="radio" value="3" name="Plan[packageId]"> <lable>10 Games</lable><br> </td>...

Union between two jquery selectors

jquery,jquery-selectors,selector
This is my case: var elems = form.find(':input'); var clEditors = form.find('.cleditor').parent(); elems.css('border', '2px'); clEditors.css('border', '2px'); Is there anyway I could prevent the duplication and do something like this: var elems = form.find(':input').union(form.find('.cleditor').parent()); elems.css('border', '2px'); I tried using a multi selector with the :parent selector and it didn't work: var...

Click handler for element with highest z-index only

javascript,jquery,html,css,jquery-selectors
In a page I'm creating there are some WebGrid components serving as master grids, and on clicking a row, a details area is shown. Now every row shall also have a delete button, so that clicking this button, the row is deleted. Now because I have a click handler for...

Is it okay to store the result of a JQuery selector in a variable?

javascript,jquery,jquery-selectors
Developers I know tend to call the same JQuery selectors over and over instead of storing the result in a variable. They are consistent with this approach. For example, they do this: var propName = $(this).attr('data-inv-name'); var propValue = $(this).attr('data-inv-value'); Instead of this: var current = $(this); var propName =...

How to trigger neighbour element to “this” object in jquery

jquery,html,css,jquery-selectors
I have 1 button - <a> link and list - ul. I need to show/hide list when I click on button. Here is the code: <div class="wide-tile" id="tile-1"> <div class="size-set"> <a href="#" class="toggle-btn"> > </a> <ul class="size-list"> <li>1x1</li> <li>1x2</li> <li>2x1</li> <li>2x2</li> </ul> </div> <a href="details.html" class="wide-tile-link"> <span class="title">About us</span> </a>...

JQuery .find() failing on an .ajax() result

jquery,html,ajax,google-chrome-extension,jquery-selectors
I'm working on a Chrome extension which, under certain conditions, performs a $.ajax() request and then uses information of specific elements in the returned HTML. For now, I'm simply trying to have it access the specific element and log it to the console to ensure it works correctly. To start,...

Select an element inside an element

javascript,jquery,jquery-selectors
Here is my DOM: <div class="a"> <div class="b">b</div> <div class="c">c</div> </div> JS: $('div.a').on('click',function(){ // ....add a div append to div.a }); $('div.b').on('click',function(){ //....edit the content of element }); Clicking div.a will add another div (d,e,f,etc.) inside div.a. Clicking div.b, div.c or other div inside will edit the content of div.b....

jQuery selector