Solved

jquery selector question

Posted on 2014-10-07
4
124 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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…

912 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now