FAQ Database Discussion Community


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

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

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

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?

Finding the name of method in jquery

jquery,jquery-selectors
I am trying to find the method names in my html code. So I will find the method name in 'onclick' attribute and then make operations. This is current code: <div class="button" style="width: 60px; float: right;" onclick="closeModalDialog('dialogAttachmentModal');">BLABLA</div> I want to write jquery like this: $('closeModalDialog').attr('id','btnCloseModal'); And I want to show...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

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

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

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

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

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

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

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

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

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

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

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

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

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

jquery select class but not id

jquery,jquery-selectors,prop
This is strange for me when I choose an element by id ; jquery code does not work , but when I choose it by classname it works great ! this code : <input name='daneshjou_number' class='daneshjou_number' type='text' > this code works : <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script> $(document).ready(function(){ $(".is_daneshjou").change(function(){ //alert("The text has...

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

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

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

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

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

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

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 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 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 apply :contains pseudo class for PhantomJS/CasperJS?

jquery-selectors,casperjs
Is there a way to select a tag containing specified text like the :contains selector does with jQuery in CasperJS?

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

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

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

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

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

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