Why is this not working?
$(document).ready(function(){
var content = 'News';
$("#test").replaceWith(function(){
return content;
});
//Here is the problem. I don't know why but I can't define adres.
var adres = $("#test .ot-origin-anchor").attr("href");
//find example.com - ugly :P
var adresRegExp = adres.match(/(\w+:\/\/+(www.|))([^/]+)/);
alert(RegExp.$3);
});
</script>
<div id="test">bnb</div>
After the .replaceWith() call, there is no element on the page with ID test. It looks like you meant to use .html() or .append() instead of .replaceWith().
var content = 'News';
$("#test").html(content);
// or
$("#test").append(content);
var adres = $("#test .ot-origin-anchor").attr("href");
replaceWith replaces every element in the selection to which it is applied. That would mean you'd end up with just content after the replace. So your next query, which looks for #test would match nothing. #test is gone. You replaced it with content.
You are replacing #test with your content variable so the selector isn't finding an element with id test. Try $("#test").html(content); instead.
Updated jsfiddle: http://jsfiddle.net/sHGrB/
Why use replace with? Use the jQuery .html() method to add it to the DOM, after that, you should be able to select it easily. Working Example.
If you replace:
$("#test").replaceWith(function(){
return content;
});
with:
$("#test").html(content);
You will get you the results you want, because #test no longer exists with replace
There are a couple problems here, one is that replaceWith is actually replacing #test with content. Then, your selector is looking for #test .ot-origin-anchor which doesn't exist, because #test was clobbered. Instead you should do something like:
$(document).ready(function(){
var content = 'News';
$("#test").html(content);
var address = $("#test .ot-origin-anchor").attr("href");
var addressRegExp = address.match(/(\w+:\/\/+(www.|))([^/]+)/);
alert(RegExp.$3);
});
Related
It looks like JQuery does the search in the current document when using a selector.
How to search for an element only inside a div element?
jQuery selectors work very much like CSS selectors, which you may be more familiar with.
First, select the div, and then descend from that:
$('#my-div').find('some-selector').
or build your selector to match children of the element in question:
$('#my-div some-selector')
Old question, but everyone seems to have missed the scoped jQuery selector (using the scope you desired, i.e. your div selector, as the second parameter)
e.g. use
var $matches = $('.adiv', '#mydiv');
This is a shorter equivalent of:
var $matches = $('#mydiv').find('.adiv');
var elems = jQuery(".foo", jQuery("#divYourWantToLimitTo") ); //BAD
//or
var elems = jQuery("#divYourWantToLimitTo .foo"); //Better
//or
var elems = jQuery("#divYourWantToLimitTo").find(".foo"); //BEST
jQuery provides several ways to search for specific elements:
$("#your_div").find(".your_things"); //Find everything inside
//-or-
$("#your_div").filter(".your_things"); //Find only the top level
//-or-
$("#your_div .your_things"); //Easiest
var elements = $('div ' + yourSearch);
$('div-selector').find('the selector-you-are-looking-for');
I have the following HTML:
<div id="new_subscribed_threads" class="block">
<h2 class="blockhead">Subscribed Threads with New Posts: (0)</h2>
I am using the $.get method to obtain the content of another page on the same server. I want to store the contents of the H2 Tag in a variable and I am confused about how to get the H2.
I tried this:
var MyVar = $(results).find("new_subscribed_threads.h2").html();
But I don't think I am on the right track.
is should be like this:
var MyVar = $(results).find("#new_subscribed_threads h2").html();
or
var MyVar = $(results).find("h2.blockhead").html();
But I don't think I am on the right track.
Yes, you're totally off actually. You don't want to use a class selector .h2, but
an id selector
descendant syntax
and an element selector
It is as simple as this:
Find the position of the h2 tag
Access it and get the content via html()
It looks something like:
var s= $('div.block >h2').html();
console.log(s);
I want to toggle a class to the html tag element. I've made it work with the body element but I cannot find the solution to also toggle a class to the html tag.
document.querySelector('[data-menu-mobile]').addEventListener('click', function(){
document.body.classList.toggle('nav-main-mobile-open');
document.html.classList.toggle('html-color-fill');
});
I know this seems to be wrong:
document.html.classList.toggle('html-color-fill');
What is the correct way to do this?
There's no document.html object, to get to the root element you should use document.documentElement.
document.documentElement.classList.toggle('html-color-fill')
This should work:
var elements = document.getElementsByClassName("myclass");
//iterate through all found elements
Array.prototype.forEach.call(elements, function(element) {
element.className = "html-color-fill";
//or remove class with:
//element.className = "";
});
What I need is to find an element which have class="selected" and than assign content of that element to a variable.
This is the content
This is the content
This is the content
Any help? Thank you
Use the class operator:
var elementContents = $('.selected').html(); //assuming there is only one element found with the class selected
Hope this helped.
var someContent = $('.selected').html();
var variable=$(".selected").text();
DEMO
Give it an Id, then use:
var content = document.getElementById("yourIdHere").innerHTML;
to ensure you only get the first occurance, don't forget to instruct jQuery to only get the first, and to ensure it's readable i'd use the .text() not the .html() selector as it will strip any HTML away if there is any
This is the content
This is the content
This is the content
<script type="text/javascript">
$(document).ready(function() {
var myVariable = $(".selected").first().text();
});
</script>
In my js I have a var in which I have stored innerHTML.
The var is having value something like
<h2>headline</h2>
<div>....</div>
...........
Now I want to retrieve value of h2 tag..what I am doing is
$(myvar).find("h2").text()
but its not working...what should be the exact syntax?
EDIT:
alert(myvar)=<h2>headline</h2>
<div>....</div>
Thanks.
The find method will not work for this case, because it gets the descendants of each element in the current set of matched elements (the h2 and the div in your example).
You can simply use filter (available on jQuery 1.3.2):
var myvar ="<h2>headline</h2>" +
"<div>....</div>";
alert($(myvar).filter('h2').text()); // headline
Check an example here.
Find() returns a collection of nodes. Use first():
h2text = $(myvar).first("h2").text();
Change your HTML to something like this.
<div>
<h2>headline</h2>
<div>....</div>
</div>
generally would make more sense to wrap the contents of your var in a div as proposed by ChaosPandion.
if that's not possible, you can try this...
<script>
var myVar = '<h2 id="test">heading</h2><div>stuff</div>';
var jVar = $(myVar);
alert($(jVar.get(0)).text());
</script>