I have a text "First Name (Given Name, Forename):" that needs to replaced with "Something" using jquery. Please see below text.
<div id="ctl00_m_g_23e3c2e9_71b9_464f_9ad3_46a603eb384f_ctl00_PanelSearch"
sizcache="3" sizset="0">
<table width="100%" sizcache="2" sizset="0">
<tbody sizcache="1" sizset="0">
<tr sizcache="0" sizset="0">
<td style="width: 40%;">
First Name (Given Name, Forename): //replace with "Something"
</td>
</tr>
</tbody>
</div>
How to do this?
If you put a class on your td (tdContent) you could do something like this:
$("#ctl00_m_g_23e3c2e9_71b9_464f_9ad3_46a603eb384f_ctl00_PanelSearch .tdContent").html("New Content goes here");
If you do not want to use a class make your selector
$("#ctl00_m_g_23e3c2e9_71b9_464f_9ad3_46a603eb384f_ctl00_PanelSearch td:first")
The simplest would be to give the td an id:
$("#tdID").html("content");
You'll want to find the DOM element and then use jQuery's text (or html) method.
$('div#ctl00_m_g_23e3c2e9_71b9_464f_9ad3_46a603eb384f_ctl00_PanelSearch td').text('Something')
You're looking for contains.
var text = $('td:contains("(Given Name, Forename)")');
text.html(text.html().replace('(Given Name, Forename)', "Something"));
update
$('td:contains("Company Name")').html('Something')
Related
How can I select the <tr> containing the child <div class="test">, as below?
<table>
<tr> <!-- this tr is what I want to select -->
<td>
<div class="test"> text </div>
</td>
</tr>
</table>
You can use parents or closest for that, depending on your needs:
$("div.test").parents("tr");
// Or
$("div.test").closest("tr");
(The initial selector can be anything that matches your div, so ".test" would be fine too.)
parents will look all the way up the tree, possibly matching multiple tr elements if you have a table within a table. closest will stop with the first tr it encounters for each of the divs.
Here's an example using closest:
Live copy | Live source
HTML:
<table>
<tr id="first"> <!-- this tr I want to select -->
<td>
<div class="test"> text </div>
</td>
</tr>
<tr id="second"> <!-- this tr I want to select -->
<td>
<div class="test"> text </div>
</td>
</tr>
<tr id="third"> <!-- this tr I want to select -->
<td>
<div class="test"> text </div>
</td>
</tr>
</table>
JavaScript:
jQuery(function($) {
var rows = $("div.test").closest("tr");
display("Matched " + rows.length + " rows:");
rows.each(function() {
display("Row '" + this.id + "'");
});
function display(msg) {
$("<p>").html(msg).appendTo(document.body);
}
});
Output:
Matched 3 rows:
Row 'first'
Row 'second'
Row 'third'
Use selector :has() like:
$("tr:has(div.test)");
Find jQuery documentation here :has() Selector
$('.test').parent('tr')
this selects exactly what you want.
you should use
$('.test').parents('tr');
For Example:
http://jsfiddle.net/7T9nN/
The below targets the parent with class of .test somewhere within its children and in the below example changes background to red...
$(document).ready(function(){
$('.test').parents('tr').css('background-color', 'red');
});
For me this is extremely powerful when trying to target exported html from indesign. Powerful because indesign does not let you tag 's but through this you can tag a and then the through this JQuery.
$('.test').parent().parent(); or $('.text').parent().closest('tr');
If I do the following is fine:
<div id="results">
<p>Hello<br>there</p>
</div>
$($("#results p").children('br').get(0).nextSibling).remove();
I get: hello
But if I do:
<th class="infobox">Head</th>
<td>Hello<br>there</td>
var newLineRemove = $(".infobox td").children('br').get(0).nextSibling();
$wikiDOM.find(newLineRemove).remove();
Gives me
Uncaught TypeError: Cannot read property 'nextSibling' of undefined
That is because .get(...) returns a DOM element not a jQuery object.
In the first example you're using $(...) to convert that DOM element to a jQuery object but you're not doing that in the second example.
This will convert the DOM element to a jQuery element and get rid of the error
var newLineRemove = $($(".infobox td").children('br').get(0).nextSibling);
But it won't do what you want it to do because as #Forty3 said "the <td> isn't inside the ..infobox"
This seems to work but I've probably made things more complicated then they have to be:
$(function(){
var td = $(".infobox").next();
if(td.find("br").length){
$(td.contents().get().reverse()).each(function(){
$(this).remove();
if(this.tagName == "BR"){
return false;
}
});
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<th class="infobox"></th>
<td>Hello<br>there</td>
</table>
I've simplest solution for this, try this one:
$('td').each(function() {
$(this).html($(this).html().split('<br>')[0]);
});
li {
margin-bottom: 10px;
}
#usp-custom-3 {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table>
<tr>
<th class="infobox"></th>
</tr>
<tr>
<td>Hell
<br>there</td>
</tr>
<tr>
<td>Hello
<br>there<br>there</td>
</tr>
</table>
Your code doesn't work because the ".infobox td" selector is looking for a td element inside an .infobox element, but in your HTML the td immediately follows the .infobox.
If you want something that is very similar to your existing JS but working with that HTML (noting that td and th elements need to be inside a tr in a table) you can do this:
$($(".infobox").next().children("br")[0].nextSibling).remove()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
<tr>
<th class="infobox"></th>
<td>Hello<br>there</td>
</tr>
</table>
That is, use .next() to get the element following the .infobox, get that element's child br elements, take the first one's .nextSibling, then wrap it in a jQuery object so that you can call .remove().
EDIT: Note that if there were multiple rows with similar elements the above code would only do the removal on the first one. If it were my code I would probably select all of the relevant elements and then update their HTML something more like this:
$(".infobox").next("td").html(function(i, h) { return h.split('<br>')[0] })
So here's my problem, I'm new to jQuery. What I am trying to do here is check for user to click on a certain table cell/row and it would then display a div named popup of an index the same as the table cell votes. Without having to make separate functions of all the rows in my table.
Using some numerical value will display all the dialogs from a click of the cell of the same value the first time and from the second time only the correct one.
I bet there's some other way to do it and maybe there's just a stupid error.
Using the index value in the click and dialog function won't work.
I am open to suggestions on improvement also.
The scripts:
<script type='text/javascript'>
$(document).ready( function() {
$('.votes').each(function(index) {
$('.votes:eq(index)').click(function() {
$('.popup:eq(index)').dialog();
});
});
});
</script>
HTML for the table part, only a snippet
<td class='votes'>5</td>
<td class='votes'>15</td>
<td class='votes'>25</td>
HTML for the div part, only a snippet of the div:
<div class='popup'>
<ul>
<li>John Johnsson</li>
<li>John Doe</li>
</ul>
</div>
<div class='popup'>
<ul>
<li>Matt Theman</li>
<li>Peter Watley</li>
</ul>
</div>
jsFiddle Demo
You don't have to iterate using each for .click, that will happen internally. You can use .index() to get the index of the element clicked with reference to its parent.
$('.votes').click(function() {
$('.popup').eq($(this).index()).dialog();
});
Initially, the main problem is that you are not using string concatenation to apply the index to the selector (demo):
$('.votes:eq(index)')
// the Sizzle selector engine doesn't know what the string "index" is.
instead of
$('.votes:eq(' + index + ')')
// using concatenation calls the .toString() method of index to apply "0" (or "1", "2", etc.)
// so that the parsed string becomes '.votes:eq(0)' which the Sizzle selector engine understands
Once the Sizzle selector engine understands which elements to target (demo), the second problem is how jQueryUI changes the DOM with the .dialog method.
Inital markup:
<table>
<tbody>
<tr>
<td class="votes">5</td>
<td class="votes">15</td>
<td class="votes">25</td>
</tr>
</tbody>
</table>
<div class="popup">
<ul>
<li>John Johnsson</li>
<li>John Doe</li>
</ul>
</div>
<div class="popup">
<ul>
<li>Matt Theman</li>
<li>Peter Watley</li>
</ul>
</div>
Once the first click event is handled, one of the div.popup elements is transformed into a jQueryUI Dialog and is appended to the body, removing it from its initial position, like so:
<table>
<tbody>
<tr>
<td class="votes">5</td>
<td class="votes">15</td>
<td class="votes">25</td>
</tr>
</tbody>
</table>
<div class="popup">
<ul>
<li>Matt Theman</li>
<li>Peter Watley</li>
</ul>
</div>
<div class="ui-dialog ui-widget ..."> ... </div>
So your initial indexes no longer apply. Fortunately, there are several solutions to both problems (a few of which I've listed below).
Solutions to Problem 1:
Use string concatenation as described above.
Use the .eq method instead, which will accept the index variable as-is
Use a delegate handler instead and grab the index from within the handler:
Example of 2:
$('.votes').eq(index);
Example of 3:
$('table').on('click', '.votes', function (e) {
var vote = $(this),
index = vote.parent().index(vote);
});
Solutions to Problem 2:
Create all of the dialogs initially and open them as needed.
Create the dialogs using a deep clone of the div element. (Not recommended)
Remove the td element to match the removed and re-appended div element. (Not recommended)
Example of 1:
var popups = [];
$('.popup').each(function (i, elem) {
var popup = $(elem).data('index', i).dialog({
"autoOpen": false
});
popups.push(popup)
});
$('table').on('click', '.votes', function (e) {
var vote = $(this),
index = vote.index();
popups[index].dialog('open');
});
I'm sure there are other solutions as well, but these are the ones I thought of of the top of my head.
Functional demo: http://jsfiddle.net/2ChvX/2/
UPDATE:
With your chosen table structure, you're actually looking for the index of the parent tr element as that is what corresponds with the div.popup element. To get the index of the parent tr element, change the line that gets the index from:
index = vote.index();
to:
index = vote.parent().index();
Updated fiddle: http://jsfiddle.net/AZpUQ/1/
Updated
FWIW, here's an example using the jQueryUI dialog (which I presume you are using?) and javascript sectionRowIndex and cellIndex.
Reusable code allowing you to identify the cell the user clicked in and perform appropriate action.
http://jsfiddle.net/KbgcL/1/
HTML:
<table id="myTable">
<tr>
<th>Label:</th>
<th>Washington</th>
<th>Idaho</th>
<th>California</th>
</tr>
<tr>
<td class='label'>Votes</td>
<td class='votes'>5</td>
<td class='votes'>15</td>
<td class='votes'>25</td>
</tr>
<tr>
<td class='label'>Voters</td>
<td class='voters'>5,000</td>
<td class='voters'>15,000</td>
<td class='voters'>25,000</td>
</tr>
</table>
<div id="msg"></div>
jQuery/javascript:
var myTr;
$('#msg').dialog({
autoOpen:false,
title: 'Report:'
});
$('#myTable tr td').click(function() {
myTr = $(this).closest('td').parent()[0].sectionRowIndex;
myCell = this.cellIndex;
myState = $('#myTable').find('tr:eq(0)').find('th:eq(' +myCell+ ')').html();
myVoters = $('#myTable').find('tr:eq(' +myTr+ ')').find('td:eq(' +myCell+ ')').html();
if (myTr==2 && myCell==3){
//California
$('#msg').html('There are ' +myVoters+ ' voters in ' +myState);
$('#msg').dialog('open');
}else if(myTr==1 && myCell==1){
$('#msg').html('There were ' +myVoters+ ' votes made in ' +myState);
$('#msg').dialog('open');
}
});
My table is
<table id="ResumeWrite" class="Vasprice" cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<th width="20%" class="bdrL_blue valignT highlight">
<span class="font18">0 to 1 year Experience</span>
</th>
<th>
<input type="button" value="submit"/>
</th>
</tr>
i want to alert the value 0 to 1 year Experience when i click the button. how to do in jquery?
You can do this by many ways. I think most simple is:
$(".font18").text();
$(".font18").html();
or
$("#ResumeWrite span.font18").text();
Last string only improves the accuracy of the finding desired item.
$('input[type="button"]')click(function(e){
e.preventDefault();
alert($(".bdrL_blue").text());
});
and you can do by also following way
$('input[type="button"]').click(function(e){
e.preventDefault();
$(this).closest('tr').find('span').text();
//or
$(this).closest('tr').find('th').text();
});
see DEMO by all THREE WAYS
Try this:
$('input[type="button"]').click(function() {
var text = $(this).closest('tr').find('span').text();
alert(text);
});
Note, I didn't use the classes on the element to select it as they appear to be style classes, and not semantically linked to the content of the element.
Example fiddle
$('#ResumeWrite input[type="button"]').click(function(){
alert($(this).parent().prev().find('span').text())
})
Demo: Fiddle
alert($(".font18").text());
or
alert($("#resumeWrite .highlight span").text());
You should read the jquery doc, and follow some tutorial, it's very basic...
var listname=$(this).parents(".x-grid3-row-table").children(".x-grid3-cell-inner.x-grid3-col-1").text();
console.log(listname);
the console.log returns following:
(an empty string)
can you help me on why listname is empty? and could you also tell me how to pass listname as parameter to a server-side method in an Ext.Ajax.request?
this is the html-code coming from my generated page:
<table class="x-grid3-row-table>
<tr>
<td class="x-grid3-col x-grid3-cell x-grid3-td-1 " tabindex="0" style="text-align: left;width: 265px;">
<div class="x-grid3-cell-inner x-grid3-col-1" unselectable="on">foo_bar#domain.de</div>
</td>
<td class="x-grid3-col x-grid3-cell x-grid3-td-2 x-grid3-cell-last " tabindex="0" style="width: 59px;">
<div class="x-grid3-cell-inner x-grid3-col-2" unselectable="on">
<span class="free">free</span>
</div>
</td>
</tr>
<tr class="x-grid3-row-body">
<!-- 7 Elements Markup down, who all act wrapping for the following -->
<div class="details">
<!-- some other markup elements, who also contain data, but are not further interesting-->
<td class="data"> foo bar detail data </td><!-- not of interest-->
<td class="edit"><input value="edit" onclick="see function on top" type="button" /> </td>
</div>
</tr>
</table>
target is to extract: foo_bar#domain.de and pass it to a server-method as parameter. the method is supposed to make a Window, but that's a different story.
the onclick call is from a expanded grid panel body, which is wrapped in a table('.x-grid3-row-table') with the given html-code.
You have a space between class names in your children selector, however your markup suggests that both the class names are of same elements.
To select an element with more than one class names the selector should not have space between class names as .classname1.className2.
var listname=$(this).parents(".x-grid3-row-table").children(".x-grid3-cell-inner.x-grid3-col-1").text();
console.log(listname);
should work.
Further clarification:
If you have space between class names as in question it would mean that you are trying to select an element having .x-grid3-col-1 class within .x-grid3-cell-inner class. Which is parent child relationship.
Edit:
If your button is in the next row you can use .prev() selector of JQuery with combination of using tr as parent selector rather than table.
listname=$(this).parents("tr").prev().children(".x-grid3-cell-inner.x-grid3-col-1").text();
Or if it is in same row then simply
listname=$(this).parents("tr").children(".x-grid3-cell-inner.x-grid3-col-1").text();
There might be a cleaner way of doing this but I came up with this as your button click handler, it assumes that the text() your looking for is one table row up from the button.
var $rows,button=this,i;
$rows=$(".x-grid3-row-table tr");
for(i=0;i<$rows.length;i++){
// get the button of that row
// not sure how to as your button is not
// in a table row with the code you posted
if($rows[i].getElementsByTagName("input")[0]===
button){
break;
}
}
// I assume the button is in a row before the button
console.log($($rows[i-1])
.find(".x-grid3-cell-inner.x-grid3-col-1")
.text()