FAQ Database Discussion Community


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() ==...

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?

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...

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.

Issue with jquery selectors within group & looping over elements

javascript,jquery,jquery-selectors
My issue is 2 fold. I have the following code: $photosContainer = $("#photos-fs") $photos = $photosContainer.find(".photo"); $length = $photos.length; if ($length > 1) { setTimeout(showNextPhoto, 3000); } function showNextPhoto() { //$current = $photos.eq(0); <-- this works //$next = $photos.eq(1); <-- this works $current = $photos.find(".current"); <-- this does not $next...

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...

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!...

Is there any meaning to 'first' pseudoclass?

jquery,jquery-selectors
I found this code: $("#some_list li a:first").click(); And I can't find any source about 'first' pseudoclass. Have anyone seen something like that ? Does it mean that first element on list is clicked ?...

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)...

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...

jQuery/Javascript get classes of multiple elements to show an element with this class

javascript,jquery,class,if-statement,jquery-selectors
I'm struggling with a jQuery live search. First of all the live search update with the following code works. //live search function function live_search(list) { $("#search") .change( function () { //getting search value var searchtext = $(this).val(); if(searchtext) { //finding If content matches with searck keyword $matches = $(list).find('a:Contains(' +...

Select all Children except first N - CSS Selector

jquery,css,jquery-selectors,css-selectors
<td> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> </td> I want to get all ps except the first 3. I am doing it like so td p:not(:nth-child(1),:nth-child(2),:nth-child(3)) But what if I want all ps except first 20? Will I have to write manually like I did above?...

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...

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...

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...

Concatenate CSS selectors

jquery,css,jquery-selectors,css-selectors
Is there a smarter way where I can use $self instead of using foo in $('.foo.bar')... ? It feels like the extra foo is redundent. I don't want to look through some selector I already found. $('.foo').on('click', function () { $self = $(this); $('.foo.bar').removeClass('bar'); // Works but feels wrong. //$self('bar').removeClass('bar');...

How can multiple variables be used to create multiple jquery selectors?

javascript,jquery,variables,jquery-selectors,addclass
When I convert multiple variables into multiple selectors the below code fails to execute. DEMO HERE $('.Btn').on('click', function () { var outer = $(this).data('test'); var inner = $(outer).children('.Inner'); $(outer + ',' + inner).addClass('Success'); }); In the code above: This is successful $(outer).addClass('Success'); This is successful $(inner).addClass('Success'); This is not $(outer...

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...

How to set array value into textfield using selectize.js

javascript,jquery,jquery-selectors,selectize.js
Hello I have a problem regarding selectize. I tried and test this code to ensure that I get a list of email coming from the database. for(var i = 0; i < e.email.length; i++){ console.log(e.email[i]); var list = e.email[i]; } The results are fine and I get what I want....

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...

How do I get the dom element which has not rendered with javascript?

javascript,jquery,dom,jquery-selectors
I have a simple js code: <html> <head> <title>test</title> <script type="text/javascript" src="js/jquery-1.11.2.min.js"></script> <script type="text/javascript"> console.log(document.querySelector('#nav-toggle')); // this will get null console.log($('#nav-toggle')); // this will get the element </script> </head> <body> <a id="nav-toggle" href="#">link</a> <script type="text/javascript"> console.log(document.querySelector('#nav-toggle')); // this will get the...

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...

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...

Jquery: How to select a specific previous id dynamically?

jquery,jquery-selectors
I'm trying to add a comment box like you could open in Facebook when you click on "comment". So I want to select the most previous id containing "users_log_id" of the current "comment" link I'm clicking on. Note: After "users_log_id" is always the id of the current id like this:...

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...

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...

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...

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...

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...

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>...

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...

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...

jQuery selector based on not having class inside

javascript,jquery,jquery-selectors
I need to select an element based on what is not inside it's element. <div class="person"> <div class="name">Jason</div> </div> <div class="person"> </div> What do I need to do to select the second div with the person class. I know there are has and not properties, do I need to combine...

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 =...

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...

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 selector precedence when using pattern matching

javascript,jquery,javascript-events,jquery-selectors,event-handling
I am creating a form that implements a bunch of similar elements. They are custom select boxes, created out of <ul>s. Some of these elements are slightly different in the way I want the mousedown event to be handled though. The way I have it set up currently is that,...

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...

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>...

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 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 Change CSS of variable div

jquery,jquery-selectors
I have a table with a bunch of td with the classes markTD and they each have a different integer for the attribute value. I would like to click the button called mark and change the color of just 1 of the td. $('#mark').click(function(){ var h2 = parseInt($('#h2').attr('value'), 10); //h2...

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....

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-' +...

`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' />...

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...

jQuery :empty not working with input

javascript,jquery,jquery-selectors
I am having a problem in using jQuery's :empty pseudo selector. Basically, it isn't working on inputs the way I think it should. Given this html, I'm trying to get a count of all the inputs which don't have values without a loop. <div id="sampleDiv"> <label>My first Input:</label> <input name="test"...

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...

Using the this keyword with jQuery selectors

jquery,jquery-selectors,this
Can someone please help me with the syntax of using the jQuery this keyword? Here is my code that works: var obj = jQuery.parseJSON(data); $('.example_infobox1').addClass(obj.gridlayout); $('.example_infobox1 .info-box').addClass(obj.boxcolor); $('.example_infobox1 .info-box-icon').addClass(obj.iconcolor); $('.example_infobox1 i').addClass(obj.icon); $('.example_infobox1 .info-box-text').html(obj.text); $('.example_infobox1 .info-box-number').html(obj.number); Here is the code that I am working on: var obj = jQuery.parseJSON(data);...

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...

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 ?...

Force angular.element .on 'change' to call specified function on the same file

angularjs,events,jquery-selectors,document
When I select say hello.png the first time, it loads it and calls handleFileSelect() function. However, if I pick the same file again, the function won't get called until I pick another filename. How can I force it to call the function? I was thinking that the modification would be...

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() ; })...

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...

Apply CSS Style Up to the First Period in Every Element with a Certain Class

javascript,jquery,jquery-selectors,substring
I'm trying to apply CSS styles to all of the characters up to the first period in the text of every element with a certain class. My initial attempt was to select the first 2 characters only, but that won't work if there are sub-items ( for example: "D (i)."...

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' ) {...

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...

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...

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...

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...

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....

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...

jQuery selector