jQuery(document).ready(function () {
var tel1 = "<p>";
var tel2 = "<h1>";
var tel3 = "<h2>";
var tel4 = "<h3>";
if(tel4.html = "855-322-8671"){
tel4.html ="<a href=\"tel:855-322-8671\">1-855-Factor-1</a>";
}
}
I am trying to figure out how to do a search and replace through the whole site for these numbers and convert it into a <a>
so you can call the number. Any ideas with JQuery, or php?
If you are trying to extract the value from all the <p>, <h1>, <h2>
and <h3>
elements then your code should look like this. I am using the <h3>
as an example.
jQuery(document).ready(function(){
var tel4 = $("h3");
})
Here we are using jQuery to target all the <h3>
elements and store the reference to those <h3>
elements in the variable var tel4
. It is a good practice to name such variables with a $
before the name, like so $tel4
.
We can then use the jQuery .each()
function to loop over all the <h3>
elements that were stored in our tel4
variable and see if any of them contain the number 855-322-8671
.
$.each(tel4, function() {
var number = "855-322-8671";
var replacement = "1-855-Factor-1"
if ($(this).is(':contains("' + number + '")')) {
$(this).html(function() {
return $(this).html().replace(number, replacement);
})
}
})
The above code will search every <h3>
that contains the number
and replace that number with our replacement
.
Hope this helps!