Solved

jquery selector question

Posted on 2014-10-07
4
130 Views
Last Modified: 2014-10-07
Hi.
I need to select the text "test", but cant figure it out :)


<div id="Price1">
<span id="SpecialOffer">122</span>
test
</div>

I´ve tried with :not, but dosent work.
0
Comment
Question by:bongii
  • 2
4 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 40365621
Test page : http://jsfiddle.net/8x41z56e/
var text = $("#Price1").contents().filter(function() { return this.nodeType == 3; }).text();
// remove carriage return
text = text.replace(/^[\r\n]+|\.|[\r\n]+$/g,"");

Open in new window

0
 

Author Comment

by:bongii
ID: 40365632
Well, If I want to hide the text or change the style, i cant use it as a var.?
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40365636
a text node don't have style, use its parent if possible or put it in a container like a span : http://jsfiddle.net/8x41z56e/1/

$("#Price1").contents().each(function() { 
    if(this.nodeType == 3) {
        $(this).wrap("<span class='style'></span>");
    }
});

Open in new window

0
 
LVL 2

Expert Comment

by:Vishal Patil
ID: 40365691
Hello Bongii,

Try with following code:
function selectText(containerid) {
        var div = document.getElementById(containerid);
        var oldstring = div.innerHTML; // Stores Current text inside Div Tag
        $(div).find('span').remove(); // Removes Span tag
        alert(div.innerHTML); // display your required text 'test'
        div.innerHTML = oldstring; // Restoring old value to div
    }

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question