Solved

jquery selector question

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

707 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

12 Experts available now in Live!

Get 1:1 Help Now