JQuery: removing char from a String / selecting

I would like to select only one class from severals with the condition that the selected class has any div-descendant with the id "#exampleId".

I thought something like this could work, but it didn't:

$(".myClass").has(div).attr("id","#exampleId").

The second problem: I have to get rid first of the hash "#". because the String (#exampleId) has been generated dynamically... It looks something like this:

var myString = "#exampleId"

And the following approach didn't work:

myString.replace('#','');

Thanks in advance

-------------Problems Reply------------

You could just pass the ID in the selector for has(). No need to remove the # either.

$(".myClass").has("div#exampleId")

Or even do it in one selector:

$(".myClass:has(div#exampleId)")

You already accepted an answer, but I'll throw this one out there anyway.

If you have more than a few .myClass elements on the page, it would probably be more efficient to select the #exampleId first, then traverse up to the first .myClass using parents().

$('#exampleId').parents('.myClass:first');

Or if the ID was in a variable, do this:

var myString = "#exampleId";

$(myString).parents('.myClass:first');

These will give you the first parent of the #exampleId that has .myClass.

You could use .closest('.myClass') if you want as well.

Category:jquery Views:0 Time:2010-08-28

Related post

  • String Unicode remove char from the string 2011-12-14

    I have a string formatted with NumberFormat instance. When i display the chars of the string i have a non-breaking space (hexa code : A0 and unicode 160). How can i remove this character from my string. I tried string = string.replaceAll("\u0160", ""

  • javascript/jquery remove or delete option from select 2010-03-11

    I need to delete an option from a select in certain circumstances. Basically: if(mystatement == true) { //remove item with id 'option1' from select of id 'select1' } Anyone know the code for me to achieve this? Many thanks. --------------Solutions---

  • Remove chars from a string 2012-02-07

    I have the following function but I want it to do the opposite eg return the bad chars not the ones I specify This function lets you specify a two strings. The string you want to parse and a string of chars that you would like to keep from the first

  • Remove chars in a string until numeric is found in ASP and VB 2010-09-29

    I am trying to add to a page that someone else has written. I need to take a string that is formated with either 3 or 4 alpha characters followed by a series of numbers. I need to remove these alpha characters so that only a string of numbers is left

  • jquery remove element and also list selection 2011-02-12

    I have a select list that when the user selects an item it builds an li element on the fly, when the users clicks the li element, the li is removed. Below is the code that I currently have, The below adds the li on select of an option $('#sectors').c

  • How to remove chars in a string, and use that for another string 2011-05-06

    I am rather new at java, and I know this cant be too difficult, but I cant quite figure it out. String xxChange = request.getParameter("XXChange"); String yyChange = request.getParameter("YYChange"); String zzChange = request.getParameter("ZZChange")

  • jQuery remove special characters from string and more 2012-01-23

    I have a string like this: var str = "I'm a very^ we!rd* Str!ng."; What I would like to do is removing all special characters from the above string and replace spaces and in case they are being typed, underscores, with a - character. The above string

  • how to remove chars into a string? 2011-07-13

    I have a lot of strings, and I need to delete '0' at start of string, but what is the better way to do it, because this is an example of strings: 0130799.jpg //I need to get 130799 0025460.jpg //I need to get 25460 Now, I'm using substr function, but

  • C remove chars from string 2011-10-19

    I have to write a function in C, which deletes all characters from string which are equal to entered character. For example user enters string "aabbccaabbcc" and char 'b' then the result should be "aaccaacc". I can't find mistake in my code (function

  • Fastest way to remove chars from string 2010-02-02

    I have a string from which I have to remove following char: '\r', '\n', and '\t'. I have tried three different ways of removing these char and benchmarked them so I can get the fastest solution. Following are the methods and there execution time when

  • Remove element from html string in JQuery 2011-12-02

    So, I am pretty stumped as to how to exclude a sub children from getting selected in JQuery... Here is my HTML structure in my string: <table> <tr> <td>some data I don't want.</td> <td> <table><tr><td>m

  • Is there a higher performance method for removing rare unwanted chars from a string? 2012-04-20

    EDIT Apologies if the original unedited question is misleading. This question is not asking how to remove Invalid XML Chars from a string, answers to that question would be better directed here. I'm not asking you to review my code. What I'm looking

  • jQuery remove SELECT options based on another SELECT selected (Need support for all browsers) 2009-08-14

    Say I have this dropdown: <select id="theOptions1"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> I want it so that when the user selects 1, this is th

  • Remove one char from a string 2009-11-17

    I'd like to remove one char from a string like this: string = "ASDFVGHFJHRSFDZFDJKUYTRDSEADFDHDS" print len(string) (33) So i would like to remove one random char in this string, and then have a len = 32 What's the best way to do so ? EDIT: thanks fo

  • is there a jquery solution for removing an item from a select dropdown by display text 2010-01-14

    I see a number of examples of removing an item from a select dropdown by the items value ( such as this question ) but is there a way to remove an item from a select dropdown if all I know is the display text? --------------Solutions------------- Try

  • jquery remove selected element and append to another 2010-03-14

    I'm trying to re-append a "removed option" to the appropriate select option menu. I have three select boxes: "Categories", "Variables", and "Target". "Categories" is a chained select, so when the user selects an option from it, the "Variables" select

  • how to remove char (") from the begin and the end of a string? 2010-04-14

    how to remove char (") from the begin and the end of a string ? ex: "1234"567" ==> 1234"567 thank's in advance --------------Solutions------------- myString = myString.Trim('"'); http://msdn.microsoft.com/en-us/library/d4tt83f9.aspx Note that this

  • python3: removing several chars from a string with a long chain of .replace().replace().replace() 2010-06-02

    I found this example on stack overflow. I understand it, but seems like a bit much for such a simple method concept... removing several chars from a string. import string exclude = set(string.punctuation) s = ''.join(ch for ch in s if ch not in exclu

  • jQuery remove SELECT options based on another SELECT selected on change and on load 2010-06-09

    I'm using the following jQuery code to remove options from a select and it is working well. But instead of it only executing when the theOption2 select is changed I would also like it to work when the page is loaded depending on the selected item tha

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.181 (s). 11 q(s)