This question already has answers here:
jQuery select element by XPath
(3 answers)
How do I detect a click outside an element?
(91 answers)
Closed 8 years ago.
Html
<div id="slideit">
google
</div>
<div>
<p>Test</p>
</div>
<div>
<p>Test</p>
</div>
Right now I am working on project where some one insist to use xPath selector.
I have similar DOM structure as i mention above. I want change color of second div.
How to use xpath selector in jQuery
This might help you.
stackoverflow link
function _x(STR_XPATH) {
var xresult = document.evaluate(STR_XPATH, document, null, XPathResult.ANY_TYPE, null);
var xnodes = [];
var xres;
while (xres = xresult.iterateNext()) {
xnodes.push(xres);
}
return xnodes;
}
$(_x('//div[#id="slideit"]/a[contains(#href, "javaScript:void(0)")]/..//following-sibling::div[1]')).css({
'color': 'red'
});
Related
This question already has answers here:
Why is document.write considered a "bad practice"?
(17 answers)
Closed 1 year ago.
Can You help me find out how to use the html button element in JavaScript because
document.write("<body> <div> </div> </body>")
is not working for buttons or drop down menus so can you help me?
my guess is the
.write
Is the problem because that probably means text and a button is not text.
This is not the most correct way to do this. You must create elements and add them to the DOM tree.
let button = document.createElement('button');
document.body.append(button);
button.onclick = () => {
alert('button clicked');
}
I think you want append(). This example is stolen from this MDN page
let parent = document.createElement("div")
let p = document.createElement("p")
parent.append(p)
console.log(parent.childNodes) // NodeList [ <p> ]
This question already has answers here:
Dynamically creating HTML elements using Javascript?
(6 answers)
Closed 2 years ago.
Trying to make an Html div inside a div that is already made using javascript but my function has a problem.
Html part
<div id="test">
<button onclick="addDiv()">test</button>
</div>
Js part
let page = document.querySelector('test');
function addDiv() {
document.createElement('div')
document.querySelector('page')
addedDiv = document.appendChild('div')
page = document.appendChild('div')
document.getElementById('div').innerHTML = "ThisFunctionIsWorking"
}
the output should be seen in the console with a text inside the div that says ThisFunctionIsWorking
but instead I get an error(Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.)
I would appreciate your time helping me...
Your code is incorrect in many ways. I suggest you study basic (web) programming with the emphasis on fundamentals like return values, scope, etc. and then basic javascript and dom tree manipulation.
A solution to your problem is:
<div id="test">
<button onclick="addDiv('test')">test</button>
</div>
function addDiv(nodeId) {
var elem = document.createElement('div')
var container = document.getElementById(nodeId)
container.appendChild(elem)
elem.innerHTML = "ThisFunctionIsWorking"
}
This question already has answers here:
Getting HTML elements by their attribute names
(8 answers)
Closed 3 years ago.
I have html page with elements:
<div id="location-id" class="form-control">
<span class="hub-trigger" data-hub-recordid="28">Germany</span>
</div>
I use var element= document.getElementById('location-id') to get html element.
Please tell me how can i get 28 from the span ?
I have tried to use var id = element.getElementByTagName('data-hub-recordid') with val(), text(), innerText() at the end but it didn't work as i expected.
Try it
document.querySelector('.hub-trigger').getAttribute('data-hub-recordid')
or it
const parent = document.querySelector('#location-id');
const child = parent.querySelector('.hub-trigger');
const recordid = child.getAttribute('data-hub-recordid');
This question already has answers here:
jQuery if div contains this text, replace that part of the text
(5 answers)
Closed 8 years ago.
...
<div>
<div>
{FirstName} {POSITION}
</div>
</div>
...
We would like find element {FirstName} and repalce him on test.
For this we make:
$("{FirstName}").replaceWith("test");
But it is not working...
Tell me please how right make replace?
What you should do is target the parent DIV (lets pretend it has a class of .test)
var content = $('.test').html();
var new_content = content.replace('{Whatever}', 'Hello');
$('.test').html(new_content);
Or in short (I haven't tested this, but it should work)
$('.test').html($('.test').html().replace('{Whatever}', 'Hello'));
If you just want to replace a string in DOM elements, the question has been asked before :
Replace all strings in dom element
But best to do this would be to use a template engine.
This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 9 years ago.
Markup:
<div class="post">
[tag]dfghf[/tag][tag]gh[/tag][tag]cxbcvb[/tag][tag]gh[/tag][tag]cxbcvb[/tag][tag]gh[/tag]
</div>
JS I have so far-
for(var j=0;j<post.length;j++){
var postText = $(post[j]);
postText.html(postText.html()
.replace('[tag]','<span class="tag">')
.replace('[/tag]','</span>')
);
}
Though this seems to only replace the very first tag. How do I get it to replace all the tags?
Try
postText.html(postText
.html()
.replace(/\[tag\]/g,'<span class="tag">')
.replace(/\[\/tag\]/g,'</span>')
);
Ex:
$('.post').html(function(){
return $(this)
.html()
.replace(/\[tag\]/g,'<span class="tag">')
.replace(/\[\/tag\]/g,'</span>')
})
Demo: Fiddle