Solved

jquery selector question

Posted on 2014-10-07
4
131 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

726 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