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
Related
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:
Strip HTML from Text JavaScript
(44 answers)
Closed 5 years ago.
I have a paragraph:
<p id="demo">Hi <b>R</b><u>o</u><i>h</i>it! It's soo good to see you</p>
How can I replace the word Rohit with Akshat in the same formatting so that I get:
Hi Ak s hat! It's soo good to see you
You can do the following:
var content = document.getElementById('demo').textContent;
document.getElementById('demo').textContent = content;
<p id="demo">Hi <b>R</b><u>o</u><i>h</i>it! It's soo good to see you</p>
Just look into this
var text = $('#demo').text();
console.log(text); //return "Hi Rohit! It's soo good to see you"
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'
});
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 can I get query string values in JavaScript?
(73 answers)
Closed 8 years ago.
<a class="test" href="www.abc.com?test1=1|6&test2=2">img</a>
<a class="test" href="www.abc.com?test2=1&test3=2">img</a>
<a class="test" href="www.abc.com?test3=1&test4=2">img</a>
I have a set of anchor elements with same class name, I need to extract a particular parameter, like test1 or test4, this value can be dynamic and update the href param with a new value
how do i do it in jquery?
any ideas?
function getUrlParameter(sParam)
{
$('.test').each( function() {
var href= $(this).attr('href');
//if href contain sParam ....... you should know that :)
);
});
}
String test1Param = getUrlParameter("test1");
Hope this help :)